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


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>


Then add this method to your code.

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

Also add self.textField.delegate = self;