How to set an action to return a key in ios

There is a view with two buttons UITextField and login. When the user is in the password text box, the return key is set to Go. How can I set this return key to activate the action from the "Login" button so that the user does not need to close the keyboard and press the "Login" button?

Thanks in advance for your reply.

+6
source share
2 answers

it's simple

  • youPasswordtextField.delegate = self; // in viewDidLoad or any suitable place

  • in your controller .h file conforms to UITextFieldDelegate protocol

3.Support delegate method

- (BOOL)textFieldShouldReturn:(UITextField *)textField // this method get called when you tap "Go" { [self loginMethod]; return YES; } -(void) loginMethod { // implement login functionality and navigate user to next screen } 
+14
source

You can use the TextField delegation method as shown below: -

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { if(self.passwordTextField isFirstResponder){ [self.passwordTextField resignFirstResponder]; //Resign the keyboard. [self loginMethod]; //call your login method here. } //Below case when user tap return key when done with login info then we move focus from login textfield to password textfield so as not making user to do this and for ease of user. else{ [self.loginTextField resignFirstResponder]; [self.passwordTextField becomeFirstResponder]; } return YES; } 

Also, be sure to set the delegate as shown below and set the UITextFieldDelegate to yourClass.h

  self.passwordTextField.delegate = self; self.loginTextField.delegate = self; 
+3
source

Source: https://habr.com/ru/post/975176/


All Articles