iOS Dismiss keyboard when user pushes the return button


Example

Setup your view controller to manage editing of text for the text field.

class MyViewController: UITextFieldDelegate {

    override viewDidLoad() {
        super.viewDidLoad()
        
        textField.delegate = self
    }

}

textFieldShouldReturn is called every time the return button on the keyboard is pressed.

Swift:

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

Objective-C:

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