29
votes

I have a view with a UITextField which should hide the keyboard when return is pressed.

My function is this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
 if ( textField == userPassword ) {
  [textField resignFirstResponder];
 }
 return YES;
}

Normally the keyboard should be hidden but it stays on the screen. resignFirstResponder is correctly called. What am I missing?

14
Have you tried returning NO? Returning YES may cause the text field to run some code which would make it first responder again. - rickharrison
Yes I did, it changed nothing. The focus however seems to be removed from the field, the (X) on the right side to clear the fields content is no longer shown after resignFirstResponder - favo
I'm having the same problem on iPad - Shaggy Frog
Same problem. Ever figured it out? - sudo rm -rf
not yet, I am still looking :( - favo

14 Answers

49
votes

I see you have the iPad tag on this. Do you happen to be presenting a modal view using UIModalPresentationFormSheet? If so, it looks like this is a limitation of the FormSheet modal presentation (either Apple is doing it intentionally for some reason, or it is a bug). See these other questions for more details:

Modal Dialog Does Not Dismiss Keyboard

Modal View Controller with keyboard on landscape iPad changes location when dismissed

33
votes

There is this helpful method which allows you to dismiss the keyboard when presenting the Modal Dialog:

 - (BOOL)disablesAutomaticKeyboardDismissal { return NO; }

This will override the default behavior of the modal dialog set by Apple and allow you dismiss the keyboard. It is in the UIViewController Class.

I hope this helps someone!

10
votes

If you are using the Interface Builder, look if your UITextField has the delegated linked with your class.

-Select your UITextField and in your Connections look if exits one connection in Outlets->delegate. If not, conect with you File's Owner Class.

This need to be linked with your File's Owner Class. This delegate tell where to search for a method. If your are overriding a method, you need to tell where the object will search for that.

7
votes

This solution worked for me after none of the above did. after calling resignFirstResponder i added a modal view & removed it.



    [myTextField resignFirstResponder];
    UIViewController *dummyController = [[UIViewController alloc] init];
    UIView *dummy = [[UIView alloc] initWithFrame:CGRectMake(-1, -1,1,1)];
    [dummyController setView:dummy];
    [self presentModalViewController:dummyController animated:NO];
    [dummyController dismissModalViewControllerAnimated:NO];

4
votes

To deal with the bug mentioned by Brandon, you can try closing and re-opening your modal view controller as long as you still have a reference to it.

[textField resignFirstResponder];
[self dismissModalViewControllerAnimated:NO];
[self presentModalViewController:yourModalViewControllerReference animated:NO];

(where "self" should be the controller you used to originally open the modal view controller)

3
votes

I was having the same problem. I realized that after connecting the delegate to the File's Owner in Interface Builder, I neglected to save in Interface Builder. Once I saved, I recompiled and the keyboard disappears correctly when hitting return.

3
votes

xcode 4.5.1

Simply click control then on the textfield drag and release on the .h file

(control key+ drag)

then in the pop up menu select

connection=acton;
name= any name;
type=id;
event=did end on exit;
arguments=sender;

then click connect button

2
votes

Did you remember to implement the UITextFieldDelegate protocol?

1
votes

I have read so many articles about this issue, where the onscreen keyboard refuses to hide when you call resignFirstResponder, but none of the suggestions worked for me.

I'm using XCode 5 (iOS 7) and have a iPhone screen containing a couple of controls which require the onscreen keyboard, but if the user clicks on the UIButton, then I want the keyboard to disappear.

enter image description here

I probably wasted one full day experimenting with resignFirstResponder and adding disablesAutomaticKeyboardDismissal functions to return NO, but nothing worked. Once the onscreen keyboard appeared, I could never get it to disappear again.

But then I had a small brainwave (as I only have a small brain).

Now, when the user clicks on my UIButton, I simply disable the UITextField and UITextView controls.

- (IBAction)btnDate_Tapped:(id)sender {
    //  The user has clicked on the "Date" button.
    self.tbClientName.enabled = NO;
    self.tbComments.editable = NO;

And suddenly, the app finds it has no editable text fields needing an onscreen keyboard, and it neatly slides the keyboard out of sight.

(Relieved sigh.)

My UIButton actually makes a popup dialog appear. When the user dismisses the popup, I re-enable these two controls, so if the user taps in one of them, the keyboard will appear again.

-(void)popoverControllerDidDismissPopover:(UIPopoverController *) popoverController {
    //  The user has closed our popup dialog.
    //  We need to make our UITextField and UITextView editable again.
    self.tbClientName.enabled = YES;
    self.tbComments.editable = YES;
    ... etc...
}

Simple, isn't it !

And surprisingly, this workaround even works on UIViewControllers which appear in Modal style.

I hope this helps other XCode victims out there.

0
votes

Based on your comment that it looks like focus has shifted, then I think what may be happening is that the keyboard is staying open for the next text input field. If your return key is a "Next" key, then returning YES for textFieldShouldReturn: will make the next textField the first responder, and keep the keyboard visible.

0
votes

The easiest way is:

  1. Go to your user interface builder,

  2. select UITextField and "Control-Drag" to "Detail View Controller-Detail" and release.

  3. The window will pop-up. Then under "Outlets" select "Delegate".

That's it. It worked for me.

0
votes

if you are in UIModalPresentationFormSheet just call

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}
0
votes

Swift 3.0:

override var disablesAutomaticKeyboardDismissal: Bool {
    get{
        return false
    }
    set {
        self.disablesAutomaticKeyboardDismissal = false
    }
}
0
votes

Swift 3.0

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

    if textField == addressTextField {
        textField.resignFirstResponder()

        return false
    }

    return true
}