Dismiss keyboard when user pushes the return button
suggest changeMake 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;
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents