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

aden picture aden · Aug 26, 2010 · Viewed 141.8k times · Source

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

Answer

Saurabh picture Saurabh · Aug 26, 2010

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;