0

iPhone App :

  • i have a screen with 3 textfields to enter name, email and location.
  • i have used grouped table view with 2 sections.
  • in tat first section has three cells.. each cell has a label n a textfield.. eg: label called name and a text box to enter name.. like wise i have it for email and location too..

HAVE DECLARED nameTextField, emailTextField, locationTextField in the interface as variables and not properties. so pls tell me how to dismiss the keyboard here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    CommonTableViewCell* cell = [[CommonTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];//
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    //cell.image = [UIImage imageNamed:@"resources_on.png"];
    cell.textLabel.frame = CGRectMake(TABLE_ROW_HEIGHT + 10, 5, self.view.frame.size.width , 25);

    if (indexPath.section == 0) {
        if (indexPath.row  == 0) {
            cell.textLabel.text = @"Username";

            nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
            [nameTextField setBackgroundColor:[UIColor clearColor]];
            [nameTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
            nameTextField.placeholder = @"Enter anonymous name";
            [nameTextField setKeyboardType: UIKeyboardTypeNamePhonePad];
            [cell addSubview:nameTextField];
            [nameTextField release];

        }else if (indexPath.row == 1) {

            cell.textLabel.text = @"Email"; 
            emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
            [emailTextField setBackgroundColor:[UIColor clearColor]];
            [emailTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
            emailTextField.placeholder = @"example@me.com";
            [emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];
            [cell addSubview:emailTextField];
            [emailTextField release];

        }else if (indexPath.row == 2) {

            cell.textLabel.text = @"Location";
            locationTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
            [locationTextField setBackgroundColor:[UIColor clearColor]];
            [locationTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
            locationTextField.placeholder = @"Fetch";
            [cell addSubview:locationTextField];
            [locationTextField release];

        }else if (indexPath.row == 3) {

            cell.textLabel.text = @"Terms of Use";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else if (indexPath.row == 4) {

            cell.textLabel.text = @"Privacy Policy";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else {

        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {

            cell.textLabel.text = @"Terms of Use";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else if (indexPath.row == 1) {

            cell.textLabel.text = @"Privacy Policy";

            cell.thumbnailView.image = [UIImage imageNamed:@"compensation_on.png"];

        }else {

        }

    }


    return cell;
}

and this isnt working for me..

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return YES; 
}
Manju Basha
  • 665
  • 1
  • 9
  • 29
  • Have you tried this http://stackoverflow.com/questions/10389476/hiding-keyboard-ios/13991703#13991703 ? – Bhavin Jan 22 '13 at 08:58
  • @user1996807: welcome to the SO. By no means i want to press you yo mark my answer as correct. But if a certain answer solves your problem it is proper to do so: others will now that the question had already been answered. And the person who wrote the answer is rewarded with some extra points :) If you're planning to use Stack Owerflow in future you should also take a look at [FAQ](http://stackoverflow.com/faq) for some guidlines. – Rok Jarc Jan 22 '13 at 09:51

2 Answers2

2

Try Google first: search?q=textfield+resignfirstresponder+not+hiding+the+keyboard

There are a lot of this kind of questions on stackoverflow.

Example:

Is this field inside a UIModalPresentationFormSheet? If so, it's a known issue that you can not dismiss the keyboard programmatically until the view controller gets dismissed.

There's a workaround for this problem, check out the question here.

From: https://stackoverflow.com/a/6854165/672989:

Community
  • 1
  • 1
Tieme
  • 62,602
  • 20
  • 102
  • 156
  • i did google a lot.. no its not inside a UIModalPresentationFormSheet. yes i saw many questions simialr to this on stackoverflow but nothing worked. – Manju Basha Jan 22 '13 at 09:12
1

You should assign a delegate to your UITextFields.

Your datasource (or any other object, but this one seems ok for the job) should conform to <UITextFieldDelegate> protocol.

 ...
 nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
 [nameTextField setBackgroundColor:[UIColor clearColor]];
 [nameTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
 nameTextField.placeholder = @"Enter anonymous name";
 [nameTextField setKeyboardType: UIKeyboardTypeNamePhonePad];

 [nameTextField setDelegate:self]; //add this line

 [cell addSubview:nameTextField];
 //[nameTextField resignFirstResponder]; this doesn't really make sense - creating the text
 //field should not (and does not) bring up the keyboard
 [nameTextField release];
 ....

Do the same for other UITextFields.

Then check if your delegate method gets called (using NSLog for example, or breakpoints):

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog (@"should return?"); 
    [textField resignFirstResponder]; 
    return YES; 
}

EDIT:

You only have to set the delegate once for each UITextField - usually when it's created.

To tell the compiler that a certain class confroms to <UITextFieldDelegate> protocol just add after the class @interface declaration in header file.

For example, if the class that you use as a dataSource for the table is named MyDataSource:

@interface MyDataSource: NSObject <UITableViewDelegate,UITextFieldDelegate>

Of course you have to implement all the required methods to correctly conform to a certain protocol. Please check the documentation: UITextFieldDelegate Protocol Reference

EDIT: as for checking the validitiy of email: you can check the validity (this is actually another question) you can do this in - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

Since your delegate object is delegate to more then one UITextField you'll have to check first if text field being edited is the one holding the email. And since your text fields are in table cells it would be hard (and unceccessary) to hold reference to all of them. You can separate email text fields from the others by using tag - let's make it 44. Modify your code slightly:

emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
emailTextField.tag = 44; //add this line

Then add the delegate method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag == 44)
    {
        //email checking code here
    }
    else
    {
        return YES;
    }
}

As for how to check email, take a look at this answer: https://stackoverflow.com/a/7707946/653513

Community
  • 1
  • 1
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
  • nameTextField resignFirstResponder] was jus a random try. forgot to delete that line. and yes it worked thank u so much. so is it tat i have to setdelegate everytime to dismiss keyboard? – Manju Basha Jan 22 '13 at 09:28
  • **AM GETTIN THIS WARNING after setting delegate..WHERE AboutMe is a class** sending 'AboutMe *' to parameter of incompatible type 'id' [3] – Manju Basha Jan 22 '13 at 09:30
  • yeah sure. also tell me if i can ignore those warnings or wat i should do regarding those warnings – Manju Basha Jan 22 '13 at 09:33
  • It's not a good practice to ignore the warnings. In this case the compiler is telling you that you didn't provide all the information it needs (you didn't officialy introduce your class as being conformant to UITextField delegate). – Rok Jarc Jan 22 '13 at 09:39
  • Yes thank you so much you really helped me a lot. Now rectified those warnings @rokjarc. Thanks a ton – Manju Basha Jan 22 '13 at 09:51
  • if u see in my code i have something called **emailTextField** so where should i include the regex code to validate the email entered by the user? – Manju Basha Jan 23 '13 at 08:02