I have two UITextFields
(e.g. username and password) but I cannot get rid of the keyboard when pressing the return key on the keyboard. How can I do this?
Tap the “123” key in the bottom-left corner of the keyboard. 3. Find the Return button on the right side of the space bar, right below the Backspace key.
This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.
First you need to conform to the UITextFieldDelegate
Protocol in your View/ViewController's header file like this:
@interface YourViewController : UIViewController <UITextFieldDelegate>
Then in your .m file you need to implement the following UITextFieldDelegate
protocol method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
[textField resignFirstResponder];
makes sure the keyboard is dismissed.
Make sure you're setting your view/viewcontroller to be the UITextField's delegate after you init the textfield in the .m:
yourTextField = [[UITextField alloc] initWithFrame:yourFrame];
//....
//....
//Setting the textField's properties
//....
//The next line is important!!
yourTextField.delegate = self; //self references the viewcontroller or view your textField is on
Implement the UITextFieldDelegate method like this:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With