09
Mar
09

Dismissing keyboard for UITextField

I found it frustrating that editable UITextFields don’t automatically dismiss the keyboard when editing is done. I think this should be the default behavior, but what do I know?
First thing you need to do is implement the UITextFieldDelegate in your UIViewController.

@interface MyViewController : UIViewController <UITextFieldDelegate> {

The next is to set the controller as the UITextField delegate (I do this in the viewDidLoad method).

- (void)viewDidLoad {

	[super viewDidLoad];

	[myTextField setDelegate:self];

}
Then, implement textFieldShouldReturn in your controller like this;
#pragma mark UITextFieldDelegate

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

	[textField resignFirstResponder];

	return YES;

}

The keyboard is the "first responder", and this causes it to go away when the "done" button is pressed (assuming you have a "done" button on your keyboard). Can anyone tell me why the numeric keypad doesn't have a done button??
About these ads

10 Responses to “Dismissing keyboard for UITextField”


  1. 1 Gareth
    March 24, 2009 at 8:19 am

    Hi what happens if you have 2 text fields ? Just dismiss both ?

    • 2 dkavanagh
      March 24, 2009 at 12:06 pm

      Yes, it will dismiss each one. that is because it refers to the text field that called it. You might want to tweak the delegate calls that move the view so that the offset (100) is based on the field position on the view.

  2. 3 dkavanagh
    March 25, 2009 at 12:22 am

    Oops! I thought your response was to another post where I mention this keyboard thing and a way to slide the view out the way of the keyboard. Sorry for the confusion.

  3. April 19, 2009 at 8:12 pm

    where is the “done” button: great question! :)
    did you ever manage to find out about the “done” button on the numeroc keypad?
    i only found this: http://stackoverflow.com/questions/584538/how-to-show-button-done-on-number-pad-on-iphone but hoping there’s a more elegant solution…?

  4. 5 Suzanna
    May 28, 2009 at 3:47 pm

    Hitting the “Done” button causes a “did end on exit” event to be generated.

    Just add the following method to your controller class:

    -(IBAction)textFieldDoneEditing:(id)sender
    {
    [sender resignFirstResponder];
    }

    In IB connect the “Did End on Exit” event of your text field to the File’s Owner icon and connect it to the above method.
    :-)

  5. 7 xyz
    October 9, 2009 at 6:26 pm

    Regarding the missing “Done” button, well, there is a simple way to manipulate the keyboard by configuring the UITextField itself:

    xTextField.keyboardType = UIKeyBoardTypeDefault;
    xTextField.returnKeyType = UIReturnKeyDone;
    xTextFiled.delegate = self;

    etc.
    The UITextField is, basically, a subView.

  6. 8 Ran
    September 2, 2010 at 10:50 am

    Thank you! This is the most helpful one I found. ^_^

  7. March 15, 2011 at 3:00 pm

    Thnx a lot!! Works great!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: