UITextField's keyboard won't dismiss. No, really

亡梦爱人 提交于 2019-12-18 12:16:53

问题


I have a table view in a modal form sheet (iPad), and one of the cells contains a UITextField. My view controller holds a reference to the text field and is also its delegate.

When the text field hits Return, I tell it to -resignFirstResponder inside of -textFieldShouldReturn:. In another case, I want to force it to end editing, so I tell the whole table view to -endEditing:YES. Afterwards I release my local reference to the text field, and reload the row to replace it with something else.

The keyboard won't go away in either case. I don't know what's wrong, and I'm not sure how to debug further. I've never had a sticky keyboard problem with any other text editing I've done-- the firstResponder resignation has always behaved as expected.

Any thoughts? Thanks.


回答1:


Implement -disablesAutomaticKeyboardDismissal and return NO. It does work on iOS 6, but you have to implement it in the right controller. If you have a modal form sheet navigation controller with a child controller that has text fields, it's the navigation controller that needs the method implementation, not the child.

(See also Dismiss keyboard on IPAD)




回答2:


The Apple docs describe this exception:

On the iPad, if a view controller modally presents its view using the "form sheet" style, the keyboard, once shown, is not hidden until the user taps the dismiss key or the modal view controller is programmatically dismissed. The purpose of this behavior is to avoid excessive animations as a user moves between views that are largely, but not entirely, text fields.

Which happens to apply here (modal form sheet on iPad). It's apparently just not possible to dismiss the keyboard in this case. Super. :\




回答3:


Since the disablesAutomaticKeyboardDismissal override isn't working on iOS6, I had to connect each text field's "Did End On Exit" event to a method and then dismiss the keyboard there, like so:

- (IBAction)doneEditing:(id)sender {

[sender endEditing:YES];

}




回答4:


The disablesAutomaticKeyboardDismissal refused to work for me on iOS 7.

But... I managed to solve this issue by simply disabling the UITextFields on the screen.

My solution is described here.

This even works on Modal UIViewControllers.




回答5:


I just found a unique situation where this occurs. I have a view that when dismissed leaves the keyboard up on the screen. I checked everything, my UITextFields delegates were connected to my view, etc. Trying to dismiss the keyboard manually in viewWillDisappear() wouldn't work, either by resignFirstResponder() on the fields or endEditing() on the view.

Then I realized it was my field validation code in the delegate methods themselves. Every time editing ended in a field I validate the text in textFieldShouldEndEditing delegate method to ensure the text is reasonable, much like this, and don't allow them to tab out of the field until it is validated.

  func textFieldShouldEndEditing(textField: UITextField) -> Bool
    {
        if self.validateField(textField) {
           return true
        } else {
          return false
        }
   }

So when a user returned to the previous view without entering any text, the validation fails, the text field is not allowed to relinquish it's first responder status, and the keyboard remains on screen as they return to the previous view.

It's a unusual situation but hopefully this will help someone else who runs into it.




回答6:


In swift just give your UITextField a delegate and generate textFieldShouldReturn(), see below for example -

class ViewController: UIViewController, UITextFieldDelegate {
    func settingUpTextField() {
        textField.delegate = self;
        return;
    }
    ...
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder();
        return true;
    }
}

done!



来源:https://stackoverflow.com/questions/8379205/uitextfields-keyboard-wont-dismiss-no-really

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!