Ios – How to hide the keyboard when I press return key in a UITextField

cocoa-touchdelegatesiosobjective-c

Clicking in a textfield makes the keyboard appear.
How do I hide it when the user presses the return key?

Best Solution

First make your file delegate for UITextField

@interface MYLoginViewController () <UITextFieldDelegate>

@end

Then add this method to your code.

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

Also add self.textField.delegate = self;