resignFirstResponder Don't work?

2019-03-29 16:38发布

问题:

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

回答1:

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


回答2:

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


回答3:

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



回答4:

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



回答5:

(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 !!