Monday, September 28, 2009 at 1:42PM Dismissing the iPhone Keyboard
Development,
iPhone,
xCode |
Niels Hansen For some reason a simple task like dismissing the keyboard for the iPhone can cause one to pull their hair out because it just will not work. There are a couple of key points to remember. The number #1 part developers forget to set is the delegate of the textField. If you're using the Interface Builder, remember to set the delegate of the textField to the File Owner.

If you're not using Interface Builder, then make sure to set the delegate of the textfield to self. I also include the returnType. For example, if the textField was called gameField:
gameField.delegate = self;
gameField.returnType = UIReturnKeyDone;
Implement the UITextFieldDelegate Implement the UITextFieldDelegate for the ViewController is required.
@interface YourViewController : UIViewController <UITextFieldDelegate>
Finally, implement the textFieldShouldReturn method and call [textField resignFirstResponder]
- (BOOL)textFieldShouldReturn:(UITextField *)textField {{
[textField resignFirstResponder];
return YES;
}
All the textFields will use this same method, so this only needs to be setup once. Make sure the delegate is set for the textField and the UITextFieldDelegate is implemented for the interface. Finally, add the textFieldShouldReturn method and call the resignFirstResponder . That's it! Now there should not be the problem of dismissing the keyboard if these few steps are followed.
Development,
iPhone 

Reader Comments (3)
-(BOOL) textFieldShouldReturn:(UITextField* {
should be
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
Thanks Frank! I updated the post to fix the piece of code!
This is hte main reason I read www.agilitesoftwzre.com. Killert posts.