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 (5)
-(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.
Thank-god I came across this, neither Apple or the dozen other posts mentions to set the delegate of the textField to the File Owner....thanks man!
Hi,
I have started using ARKit downloaded from https://github.com/nielswh/iPhone-AR-Toolkit to view predefined map points on iPhone. So far its cool but I have noticed that when I have couple of map points close together, ARKit overlaps their windows frames with each other and we can't see the clear information. Is there any way to get rid of it so that windows frames don't overlap and they stake on one another?
Thanks.
Paresh