2

Possible Duplicate:
How to make return key on iphone make keyboard disappear?

I have two text fields in the page I'm working on. Only the first text field disappears after I click return, yet it doesn't work for the second text field.

Here are the methods in my .m file:

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated]; 
    self.cardNameTextField.delegate = self;    
    self.pinTextField.delegate = self;
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField
{    
    if (_cardNameTextField !=nil)
    {
        [self.cardNameTextField resignFirstResponder];
    }
    else if (_pinTextField !=nil)
    {      
        [self.pinTextField resignFirstResponder];
    } 
    return YES;
}

Advice please :)

Community
  • 1
  • 1
Hans
  • 79
  • 2
  • 7

6 Answers6

5

Replace above code with this and try if it helps:

-(BOOL) textFieldShouldReturn:(UITextField *)textField{

    if ([_cardNameTextField isEditing]) {

    [self.pinTextField resignFirstResponder];
    }

     if ([_pinTextField isEditing]) {

    [self.cardNameTextField resignFirstResponder];

    }

    return YES;
    }
Neelam Verma
  • 3,232
  • 1
  • 22
  • 33
1

Make sure you have set the delegate of the text field.

Deviator
  • 688
  • 3
  • 7
1

The problem with your code was that you were checking if your UITextFields were nil or not, which aren't equal to nil. So whenever the conditions were checked, it always went into the first case and returned (the other being in the else). The following scenarios will also work:

-(BOOL) textFieldShouldReturn:(UITextField *)textField
{    
    [textField resignFirstResponder];   
    return YES;
}

OR

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
 if (_cardNameTextField == textField)
 {
    [self.cardNameTextField resignFirstResponder];
 }
 else if (_pinTextField == textField)
 {      
    [self.pinTextField resignFirstResponder];
 } 
return YES;
}

OR

Just remove the else keyword in the second case.

OR

Do it @Neelam Verma's way.

I would recommend the first scenario which forces the first responder to be resigned without checking for any conditions...

tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
0

Use the delegate UITextFieldDelegate

And in your implementation class, you have two steps:

  1. set the delegate

    self.textField.delegate = self;

  2. implement this method

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
       [textField resignFirstResponder];
       return YES;
    }
    
Patrick Santana
  • 397
  • 3
  • 17
0

When the user taps the Done button on the text keyboard, a Did End On Exit event will be generated, and at that time, we need to tell the text field to give up control so that the keyboard will go away. In order to do that, we need to add an action method to our controller class. how about this:

in your . h :

- (IBAction)textFieldDoneEditing:(id)sender;

in your .m:

- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
Bazinga
  • 2,456
  • 33
  • 76
0

This may helps you!

- (void) textFieldDidEndEditing:(UITextField *) textField
{
    [textField2 resignFirstResponder];
}
Hemang
  • 26,840
  • 19
  • 119
  • 186
MobileApp Developer
  • 384
  • 1
  • 3
  • 12