« Xcode 3.2: The New Find Redesign | Main | iPhone TableView: Swiping a cell to delete »
Monday
Sep282009

Dismissing the iPhone Keyboard

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.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (5)

-(BOOL) textFieldShouldReturn:(UITextField* {
should be
- (BOOL)textFieldShouldReturn:(UITextField *)textField {

October 29, 2009 | Unregistered CommenterFrank

Thanks Frank! I updated the post to fix the piece of code!

December 27, 2009 | Registered CommenterNiels Hansen

This is hte main reason I read www.agilitesoftwzre.com. Killert posts.

March 9, 2010 | Unregistered CommenterColeen

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!

April 25, 2010 | Unregistered CommenterDermot

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

February 23, 2011 | Unregistered CommenterRaj

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>