Search code examples
iosobjective-ccocoa-touchdelegates

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?


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;