No longer able to hide keyboard during viewWillDis

2019-06-15 00:58发布

The following code used to work in iOS6 to hide the keyboard when a view controller was popped off of the navigation stack:

- (void)viewWillDisappear:(BOOL)animated {
    [self.view endEditing:YES];
    [super viewWillDisappear:animated];
}

However, in iOS7, the [self.view endEditing:YES] line seems to get ignored. I tried the command in other view events (viewDidDisappear, viewWillAppear, and viewDidAppear), and the only one it worked in is viewDidAppear. It seems that once a "pop" is initiated, we lose the ability to hide the keyboard until the view controller is "pushed" back on the stack.

While placing the code in viewDidAppear does work to hide the keyboard, the bad thing is that the keyboard is displayed briefly when the viewController is pushed back on to the navigation stack...pretty unacceptable from a UI perspective.

Has anyone else had success in working around this issue? I would prefer not to have to write my own CANCEL button, but right now, that is the only thing I can think of that will work.

7条回答
▲ chillily
2楼-- · 2019-06-15 01:27

Put your UItextfield or UItextview for global declaration.

UITextfield textfield = your textfield object;

-(void)viewWillDisappear:(BOOL)animated 
{

    [self.view endEditing:YES];
    [textfield resignFirstResponder];
    [super viewWillDisappear:animated];
}
查看更多
登录 后发表回答