1
votes

I have a storyboard scene that only has two UITextField.The first one is left untouched, fresh out of the object library. The second one has its delegate outlet connected to the ViewController's code.

App's interface

The second text field's connection in IB

(Sorry I have to post images this way, because StackOverflow won't let me.)

The code is really simple. This is all of it.

class ViewController: UIViewController, UITextFieldDelegate {    
    func textFieldDidBeginEditing(textField: UITextField) {
        textField.resignFirstResponder()
    }
}

The only thing it does is dismiss the keyboard when it's about to come up.

The first one shows the keyboard normally. The second one doesn't show keyboard because it's immediately dismissed.

It all works perfectly on their own, until I click the first text field to bring up the keyboard, then with the keyboard on screen, I click the second text field. What happens next, is that the second text field loses its focus, but the keyboard remains on screen.

Using print(:), I've verified that, when I call resignFirstResponder on the second text field, it is the first responder, and the return value of that call is true, and after the call it is no longer the first responder.

But the keyboard is still there...

I tried adding print(:) in the delegate's textFieldShouldReturn(:) method, and clicking the return key after the text field resigned. There was nothing printed in the debug console.

I tried disablesAutomaticKeyboardDismissal(), returning neither false nor true produces any difference.

Edit

What I want is simple: I want the first text field to do whatever it's default to do, and the second one to dismiss its keyboard as soon as it's tapped (from the user's perspective, basically meaning not showing it).

I know textFieldShouldBeginEditing(:), but I can't use it for various reasons, I just have to dismiss the keyboard after it's been activated.

Edit Again

I tried view.endEditing(true) as well. In fact, I tried .endEditing(true) on every view in the scene. Doesn't work.

1

1 Answers

2
votes

Try smth like that:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    view.endEditing(true)
    return true
}

func textFieldDidBeginEditing(textField: UITextField) {
    textField.resignFirstResponder()
    //make here whatever you like

}