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

9 thoughts on “Dismissing keyboard for UITextField

    1. 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.

  1. 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.

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

    Just add the following method to your controller class:

    [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.


  3. 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;

    The UITextField is, basically, a subView.

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 )

Facebook photo

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

Connecting to %s