51

This is probably a dumb question, but I can't find the answer in the docs. Did the "Done" button on the pop-up keyboard always cause the keyboard to disappear? I see a lot of code around the web like this:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];
    return YES;
}

When I press the "Done" button, the keyboard pops down and the UITextField resigns first responder.

I'm presuming that pressing the "Done" button didn't used to cause a UITextField to resignFirstResponder, but that behavior changed at some time.

I'm debugging on OS 3.0 - 3.1.3

Max Desiatov
  • 5,087
  • 3
  • 48
  • 56
nevan king
  • 112,709
  • 45
  • 203
  • 241
  • 1
    See https://developer.apple.com/library/ios/#documentation/iphone/conceptual/iPhone101/Articles/05_ConfiguringView.html –  Feb 09 '12 at 22:13
  • 1
    Apparently I don't have enough rep to just add a comment to nevan king's answer, so I'll mention here that for his solution it's not necessary to make the class a UITextFieldDelegate. The UITextFieldDelegate protocol just defines some messages sent to the delegate asking whether the text field should resign, that it did resign etc... Any object can receive actions in the way his answer has been implemented simply by defining their method in the way he's done with textFieldFinished. – Robert Hawkey Jul 27 '12 at 15:16

5 Answers5

93

I made a small test project with just a UITextField and this code

#import <UIKit/UIKit.h>
@interface TextFieldTestViewController : UIViewController
<UITextFieldDelegate>
{
    UITextField *textField;
}
@property (nonatomic, retain) IBOutlet UITextField *textField;
@end

#import "TextFieldTestViewController.h"
@implementation TextFieldTestViewController
@synthesize textField;

- (void)viewDidLoad
{
    [self.textField setDelegate:self];
    [self.textField setReturnKeyType:UIReturnKeyDone];
    [self.textField addTarget:self
                  action:@selector(textFieldFinished:)
        forControlEvents:UIControlEventEditingDidEndOnExit];
    [super viewDidLoad];
}
- (IBAction)textFieldFinished:(id)sender
{
    // [sender resignFirstResponder];
}

- (void)dealloc {
    [super dealloc];
}
@end

The text field is an unmodified UITextField dragged onto the NIB, with the outlet connected.
After loading the app, clicking in the text field brings up the keyboard. Pressing the "Done" button makes the text field lose focus and animates out the keyboard. Note that the advice around the web is to always use [sender resignFirstResponder] but this works without it.

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • Hmm. And what's addTarget:action:forControlEvents: for? Isn't setDelegate: enough? Let me try it by myself. – bealex May 13 '10 at 21:28
  • Yes, exactly. If you'll remove addTarget:… call, everything goes ok. – bealex May 13 '10 at 21:39
  • 1
    For noobs like me, the way to do this with interface builder is to drag the + from the "Did End On Exit" to the file's owner and then assign it to an IBAction that does nothing (textFieldFinished:(id)sender in this example). The answer is above is correct, just wanted to add to it. – Paul Wieland Sep 14 '11 at 17:29
  • If it still doesn't work, check out this question: http://stackoverflow.com/questions/3372333/ipad-keyboard-will-not-dismiss-if-modal-view-controller-presentation-style-is-ui – Vladimir Grigorov Sep 18 '12 at 14:48
15

In Xcode 5.1

Enable Done Button

  • In Attributes Inspector for the UITextField in Storyboard find the field "Return Key" and select "Done"

Hide Keyboard when Done is pressed

  • In Storyboard make your ViewController the delegate for the UITextField
  • Add this method to your ViewController

    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        return YES;
    }
    
bickster
  • 1,292
  • 17
  • 18
  • Adding swift code for reference: `func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true }` – Rom Nov 29 '18 at 19:03
6

From the documentation (any version):

It is your application’s responsibility to dismiss the keyboard at the time of your choosing. You might dismiss the keyboard in response to a specific user action, such as the user tapping a particular button in your user interface. You might also configure your text field delegate to dismiss the keyboard when the user presses the “return” key on the keyboard itself. To dismiss the keyboard, send the resignFirstResponder message to the text field that is currently the first responder. Doing so causes the text field object to end the current editing session (with the delegate object’s consent) and hide the keyboard.

So, you have to send resignFirstResponder somehow. But there is a possibility that textfield loses focus another way during processing of textFieldShouldReturn: message. This also will cause keyboard to disappear.

bealex
  • 10,004
  • 1
  • 21
  • 27
  • Thanks for the answer. I found the part that's causing the `UITextField` to resign first responder. I have a call ` [textField addTarget: self action: @selector(textFieldDone:) forControlEvents: UIControlEventEditingDidEndOnExit]`. Strangely, the `textFieldDone:` method is empty (I deleted the `resignFirstResponder` call). So I still can't figure out why the keyboard disappears. – nevan king May 13 '10 at 18:31
  • Maybe you move focus to another component implicitly? By changing some value in a table or a label for example? – bealex May 13 '10 at 19:35
  • I don't think so there's anything else going on. I created a small project (posted the code below) and it gives the same behavior. Not really sure why it's happening. – nevan king May 13 '10 at 21:12
  • nevan, your solution works. I had a textfield in a tableview cell with a keyboard with done key. On empty input, the done key would not shut down the keyboard. After I added the addTarget code, it is now dismissing the keyboard. – CF_Maintainer Jun 07 '11 at 20:10
2

One line code for Done button:-

[yourTextField setReturnKeyType:UIReturnKeyDone];

And add action method on valueChanged of TextField and add this line-

[yourTextField resignFirstResponder];
Matt
  • 435
  • 5
  • 15
mmx
  • 39
  • 2
0

I used this method to change choosing Text Field

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

if ([textField isEqual:self.emailRegisterTextField]) {

    [self.usernameRegisterTextField becomeFirstResponder];

} else if ([textField isEqual:self.usernameRegisterTextField]) {

    [self.passwordRegisterTextField becomeFirstResponder];

} else {

    [textField resignFirstResponder];

    // To click button for registration when you clicking button "Done" on the keyboard
    [self createMyAccount:self.registrationButton];
}

return YES;

}

EddieG
  • 49
  • 1
  • 5