Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set action to return key in ios

There is view with 2 UITextField and login button. When user is in password textbox keyboard is shown with return key set as "Go". How i can set that return key to make action from Login button so user don't need to close keyboard and touch Login button?

Thanx in advance for reply.

like image 888
Nuriddin Avatar asked Sep 12 '14 07:09

Nuriddin


People also ask

What keyboard key is return?

keys on the lower right of the numeric keypad, while the return key is situated on the right edge of the main alphanumeric portion of the keyboard. On ISO and JIS keyboards, return is a stepped double-height key spanning the second and third rows, below ⌫ Backspace and above the right-hand ⇧ Shift .

How do you dismiss a keyboard in Swift?

Via Tap Gesture 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.


2 Answers

it's simple

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

  2. in your controllers .h file conform to UITextFieldDelegate protocol

3.implement 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
}
like image 181
Shaik Riyaz Avatar answered Oct 17 '22 04:10

Shaik Riyaz


You could use the TextField Delegate method as 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, don't forgt to set delegate as below along with setting UITextFieldDelegate in yourClass.h

 self.passwordTextField.delegate = self;
 self.loginTextField.delegate = self;
like image 43
nikhil84 Avatar answered Oct 17 '22 05:10

nikhil84