This form does not yet contain any fields.
    « Xcode 3.2: The New Find Redesign | Main | iPhone TableView: Swiping a cell to delete »
    Monday
    28Sep2009

    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 (3)

    -(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

    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>