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 🙂
The problem with your code was that you were checking if your
UITextFields werenilor not, which aren’t equal tonil. 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:OR
OR
Just remove the
elsekeyword 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…