Dismiss keyboard when user pushes the return button

suggest change

Make the view controller the text field’s delegate. textField is that field. textFieldShouldReturn runs when the keyboard’s return key is pressed.

class MyViewController: UIViewController, UITextFieldDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        textField.delegate = self
    }

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

Objective-C:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
   [textField resignFirstResponder];
   return YES;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents