Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin iOS - Navigate to next text field with return key

Tags:

ios

xamarin

I couldn't find anything in the Xamarin documentation about navigating to the next text field in a series of text fields on a form, only a small tutorial on removing the keyboard.

To keep things simple I am using a txtUsername(tag 1) text field and a txtPassword(tag 2) text field. When I implement the following code, it isn't transferring to Xamarin Studio. Does anyone know the way this can be done code in Xamarin Studio, or alternatively if I can transfer the code from XCode to Xamarin Studio.

I am using the following code:

- (BOOL)textFieldShouldReturn:(UITextField *)txtUsername{
    NSLog(@"textFieldShouldReturn:");
    if (txtUsername.tag == 1) {
        UITextField *txtPassword= (UITextField *)[self.view viewWithTag:2];
        [txtPassword becomeFirstResponder];
    }
    else {
        [txtUsername resignFirstResponder];
    }
    return YES;
}

Thanks in advance

like image 250
Jonathan Bick Avatar asked Jul 22 '13 08:07

Jonathan Bick


1 Answers

I think that the simplest way to do this is using the ShouldReturn method on your UITextFields with the BecomeFirstResponder method. For example, for a login form with Username and Password UITextFields, as follows (in your ViewDidLoad method):

Username = new UITextField();
Username.ReturnKeyType = UIReturnKeyType.Next;
Username.KeyboardType = UIKeyboardType.EmailAddress;
Username.ShouldReturn = (tf) =>
    {
        Password.BecomeFirstResponder();
        return true;
    };
View.Add(Username);

Password = new UITextField();
Password.SecureTextEntry = true;
Password.ReturnKeyType = UIReturnKeyType.Go;
Password.ShouldReturn = (tf) =>
    {
        // Do your login
        return true;
    };
View.Add(Password);

When you click next when the Username is active, it moves to the Password field. Clicking Go in the password field submits the form.

like image 179
David Conlisk Avatar answered Sep 20 '22 20:09

David Conlisk