29

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?

Moshe
  • 57,511
  • 78
  • 272
  • 425
favo
  • 5,426
  • 9
  • 42
  • 61

14 Answers14

49

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

Community
  • 1
  • 1
Brandon
  • 953
  • 10
  • 12
  • thats exactly what is happening, thank you, I accepted your answer because it explains that this is some Apple behaviour/bug – favo Feb 27 '11 at 15:10
  • 1
    Wow, it took me a while to find this answer and it works perfectly. I had no ide it was related to a UIModalPresentationFormSheet. – zachzurn May 03 '12 at 07:03
  • 5
    tony.stack's answer actually fixes the issue. – Daniel T. Aug 16 '12 at 15:37
  • 1
    I solved this by executing `resignFirstResponder` in the main queue.. Hope this helps anyone – Matej Nov 29 '14 at 12:55
33

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!

jomo
  • 14,121
  • 4
  • 29
  • 30
tony.stack
  • 780
  • 8
  • 21
10

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.

Marcos Issler
  • 115
  • 1
  • 9
  • The responder is correctly called, its not a matter of the delegate. Thank you for trying :-) – favo Aug 14 '10 at 16:21
7

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];

gmeroz
  • 211
  • 4
  • 9
  • Keyboard animation is gone (as are my animations associated with it) but yeah, this is the only solution that works for me. – Kalle Dec 04 '14 at 06:01
4

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)

drynaski
  • 121
  • 1
  • 5
3

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

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
madusanka
  • 41
  • 2
3

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.

jmundie
  • 31
  • 1
2

Did you remember to implement the UITextFieldDelegate protocol?

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • 1
    If you implement the UITextFieldDelegate protocol you can inadvertently cause this behavior if you do text validation. If your validation codes returns false from textFieldShouldEndEditing when the text is invalid, the field can't relinquish it's firstResponder status and the keyboard will remain on screen in the next view. More details at http://stackoverflow.com/questions/8379205/uitextfields-keyboard-wont-dismiss-no-really/37082876#37082876 – SafeFastExpressive May 06 '16 at 23:44
1

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.

Mason
  • 6,893
  • 15
  • 71
  • 115
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
  • Please help Ive also been struggling with this for a full day now! I have done everything you've said is there anything else that could possible differ in your app that i can change in mine? – 4GetFullOf Apr 22 '14 at 22:10
  • My UIButton is shown in the screenshot above (showing the date/time). When the user clicks on this, I wanted the onscren keyboard to disappear, and a popup to appear, where they could select a date. – Mike Gledhill May 09 '14 at 08:46
0

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.

atxe
  • 5,029
  • 2
  • 36
  • 50
0

if you are in UIModalPresentationFormSheet just call

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Jeremy Piednoel
  • 407
  • 2
  • 5
  • 12
0

Swift 3.0:

override var disablesAutomaticKeyboardDismissal: Bool {
    get{
        return false
    }
    set {
        self.disablesAutomaticKeyboardDismissal = false
    }
}
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
0

Swift 3.0

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

    if textField == addressTextField {
        textField.resignFirstResponder()

        return false
    }

    return true
}
krish
  • 3,856
  • 2
  • 24
  • 28
0

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.

Chris Garrett
  • 4,824
  • 1
  • 34
  • 49