Tyler Hilbert Tyler Hilbert - 1 month ago 9
iOS Question

Objective C close keyboard when return key is pressed

I have a button that when pressed brings up a keyboard for a textfield:

- (IBAction)textButtonPress:(id)sender {
[self.textField becomeFirstResponder];
}


The problem I have is that when I press the return button on the keyboard nothing happens. How can I make the keyboard automatically close when the return key is pressed?

Answer

Add this in your ViewController class

yourTextField.delegate = self

and this UITextFieldDelegate method

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