5

I try to hide keyboard on iPad but I don't know why resignFirstResponder don't work. But popToRoot has work well.

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{
    NSString *desc = [NSString stringWithFormat:@"%@",[descTF text]];
    [textField resignFirstResponder];
    [self.navigationController popToRootViewControllerAnimated:YES];


    return YES;
}

So Could you guide me what should I do please ??

3
  • I try to put - (BOOL)disablesAutomaticKeyboardDismissal { return NO; } on my code under - (BOOL)textFieldShouldReturn:(UITextField *)textField{...} but Keyboard still didn't hide. Jul 28, 2011 at 4:31
  • in the link I post, the problem might be a bug from Apple.
    – Anh Pham
    Jul 28, 2011 at 4:47

5 Answers 5

19

Is this field inside a UIModalPresentationFormSheet? If so, it's a known issue that you can not dismiss the keyboard programmatically until the view controller gets dismissed.

UPDATE: according to this thread from the Apple Developer Forums, a possible workaround for this is to present the sheet view control from inside a navigation controller subclass that implements the disablesAutomaticKeyboardDismissal method. So something like:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
CustomNavigationController *navController = [[CustomNavigationController alloc] initWithRootViewController:myViewController];
theNavigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:theNavigationController animated:YES];
1
  • Added possible workaround and link to Apple Developer Forums thread. Aug 2, 2011 at 6:51
7

To follow up on samvermette's answer, if it is inside a UIModalPresentationFormSheet you can now override the disablesAutomaticKeyboardDismissal method to get the behavior you desire. Add the following method to your class and the UITextFields and UITextViews will respond to resignFirstResponder.

- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}
1
  • 1
    +1 This should really be the selected answer (obviously the original selected answer was correct at one time). Thanks BTW.
    – Firo
    Aug 9, 2013 at 22:41
1

You should never have occasion to send -resignFirstResponder. What you should do is just send -endEditing:YES to the view in question.

0

Did you implement the UITextFieldDelegate ? If you had implemented that then this, then it should work.

3
  • I already add <UITextFieldDelegate>. And also link delete @xib file. T^T But it still didn't hide. Jul 28, 2011 at 4:33
  • textField.returnKeyType = UIReturnKeyDone ?
    – Legolas
    Jul 28, 2011 at 4:36
  • errr Could you guide me what should I do for "textField.returnKeyType = UIReturnKeyDone" ??? Jul 28, 2011 at 10:00
0

(Apologies... this is the fourth place on the StackOverflow site where I'm posted this fix..)

I had huge problems with XCode 5 & iOS 7, trying to get the onscreen keyboard to hide.

Eventually, I found a very simple solution: I just disabled the UITextFields in my form. Magically, this was enough to make the onscreen keyboard slide away.

More details here

Surprisingly, this even works on Modal UIViewControllers. Yeah, it surprised me aswell !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.