keyboard done key action swift iOS doesn't work keyboard done key action swift iOS doesn't work ios ios

keyboard done key action swift iOS doesn't work


You need to implement delegate method which is called when you hit done button:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {    textField.resignFirstResponder()    return true}

You also need to conform to UITextFieldDelegate protocol:

// I assume you override UIViewController class. If not add UITextFieldDelegate to your classclass MyViewController: UIViewController, UITextFieldDelegate

The last thing is set up your class to be a text field delegate:

textField.delegate = self


textField.delegate = self

can be replaced by enter image description here

This will create the necessary connections between your View, its component and will make the textFieldShouldReturn method work as expected.


The protocol methods have new signatures (Swift 4.1). IE:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {    textField.resignFirstResponder()    return true}

As the protocol methods are optional, using a wrong signature will silently fail.


matomo