Showing posts with label UITextFieldDelegate. Show all posts
Showing posts with label UITextFieldDelegate. Show all posts

Sunday, 13 March 2011

Hide Keyboard on Text Field Return - iPhone, iPad, iOS Development

One of the most asked question's by new iPhone developers is how to hide the keyboard. This is certainly one of the most lacking area's of the documentation for a budding iPhone developer.

To programatically hide the keyboard you must call the method resignFirstResponder on your text field or text view. This can be called from anywhere however it is most commonly from a done button or from the keyboard return key being pressed.
  [textField resignFirstResponder];
To hide the keyboard when the return key is pressed you must add a listener to the text field (UITextField) delegate method. UITextfieldDelegate protocol contains a method called textFieldShouldReturn, This method is called to determine if the text field should process the pressing of the return key. You can call your resignFirstResponder on the text field from within this method to hide the keyboard when pressed.

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


If this doesn't appear to be working for you make sure you've remembered to set the delegate property on the textField.

If you have any problems feel free to post in the comment's ;)