Question

Right now I have a simple Login view controller. It has 2 text fields, one for a username and one for a password. I would like to add the following functionality to the keyboards for these 2 text fields:

  1. When the user is done typing their username in the username text field, they should be able to press the "Next" return key which should take them to the 2nd text field for the password.

  2. When the user is done typing their password and they press the keyboard's "Done" return key, I want to perform an IBAction that I have setup.

Here is the IBAction code that I want to perform when the user presses the "Done" return key in the password text field:

-(IBAction)didTapLoginButton:(id)sender {

    [self textFieldShouldReturn:_usernameEntry];

    NSString *user = [_usernameEntry text];
    NSString *pass = [_passwordEntry text];

    if ([user length] < 4 || [pass length] < 4) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Entry" message:@"Username and Password must both be at least 4 characters long." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    } else {
        [_activityIndicator startAnimating];
        [PFUser logInWithUsernameInBackground:user password:pass block:^(PFUser *user, NSError *error) {
            [_activityIndicator stopAnimating];
            if (user) {
                NSLog(@"Successful login");

                //[self performSegueWithIdentifier:@"loginToMainAppSegue" sender:self];
                [self performSegueWithIdentifier:@"loginToMediaCaptureVC" sender:self];


            } else {
                NSLog(@"%@",error);
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed." message:@"Invalid Username and/or Password." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
                [alert show];
            }
        }];
    }

}

I have already set my view controller as the delegate and both of the text fields work perfectly, but I would like to add the extra functionality that I have listed above.

If it makes a difference, the text fields and keyboards are not being created programatically. They were created on the storyboard.

Thanks for the help.

Was it helpful?

Solution

step1 - give tag to both your text field

step2 - check the tag in - (BOOL)textFieldShouldReturn:(UITextField *)textField;

if first username is active make the password textfield becomes first responder. if password field is active resign first responder

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
  if([_txtFieldUserName isFirstResponder]){

    [_txtFieldPassword becomeFirstResponder];
  }
  else if ([_txtFieldPassword isFirstResponder]){

    [_txtFieldPassword resignFirstResponder];
  }
}

OTHER TIPS

For a UITextField or editable UITextVie you can also do:

[myTextField setReturnKeyType:UIReturnKeyNext];

You can also configure this in Interface Builder, under Text Input Traits for your text field/view.

UPDATE

In Xamarin.iOS or Monotouch is:

myTextField.ReturnKeyType = UIReturnKeyType.Next;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top