resignFirstResponder Don't work?

2019-03-29 16:17发布

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

5条回答
相关推荐>>
2楼-- · 2019-03-29 17:00

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];
查看更多
劫难
3楼-- · 2019-03-29 17:02

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;
}
查看更多
SAY GOODBYE
4楼-- · 2019-03-29 17:08

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

查看更多
\"骚年 ilove
5楼-- · 2019-03-29 17:10

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

查看更多
做个烂人
6楼-- · 2019-03-29 17:18

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

查看更多
登录 后发表回答