I am trying to make the keyboard disappear when a UIButton is pressed -
-(IBAction)nextButtonPressed{
[usernameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];
}
I have declared the button as IBOutlet and connected it to the IBAction in the storyboard. But the code however does not work and keyboard remains visible. What am I doing wrong ?
First you need to subscribe to the UITextFieldDelegate Protocol in your View/ViewController's header file like this:
@interface YourViewController : UIViewController <UITextFieldDelegate>
Make sure you're setting your view/viewcontroller to be the UITextField's delegate after you init the textfield in the .m:
yourTextField.delegate = self;
And you can do this :
-(IBAction)nextButtonPressed{
[usernameTextField resignFirstResponder];
[passwordTextField resignFirstResponder];
}
Hope it'll help
You forgot this:
usernameTextField.delegate = self;
passwordTextField.delegate = self;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.