2
votes

I have a UITextField and a UIButton. If the button is tapped while the text field is active, I want to dismiss the keyboard, but I'm unable to do so. I tried calling [textField resignFirstResponder] and [textField endEditing:YES] when the button is tapped, but they have no effect -- [textField isFirstResponder] returns false, so it's not surprising that resignFirstResponder has no effect. But in that case, why isn't the keyboard disappearing? Thanks.

3
Also, if I push a view while the keyboard is active, the keyboard is shown again when the view is popped. Does UIKit remember which view is the first responder and restore the first responder status to that view when its view controller is shown? - Kartick Vaddadi
Finally, is there a clean way to have the keyboard disappear if the user taps or swipes elsewhere? Something like disableKeyboardOnFocusLoss would be ideal :) Thanks. - Kartick Vaddadi

3 Answers

1
votes

Make sure your "textField" class member is properly hooked up in the nib file. NSLog it's value right before you call firstResponder and see if it's nil. Also, enter text into the textfield and see that you can properly print it out with NSLog at the same point you're calling resignFirstResponder. Usually when a control does not respond correctly it's because IBOutlets aren't hooked up properly.

0
votes

Try making an IBAction

-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}

And connect it to the UIButton

0
votes

have you added <UITextFieldDelegate> in your .H ??