Xamarin iOS - Navigate to next text field with return key

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.


I wrote a generic solution for every form (not only those with 2 fields).

I´ve got a BaseViewController with these methods:

private UITextField[] formTextfields;
protected void EnableNextKeyForTextFields(UITextField[] fields)
    {
        formTextfields = fields;

        foreach (var field in fields)
        {
            field.ShouldReturn += ShouldReturn;
        }
    }

    private bool ShouldReturn(UITextField textField)
    {
        int index = Array.IndexOf(formTextfields, textField);

        if (index > -1 && index < formTextfields.Length - 1)
        {
            formTextfields[index + 1].BecomeFirstResponder();
            return true;
        }
        else if (index == formTextfields.Length - 1)
        {
            formTextfields[index].ResignFirstResponder();
            FormFinished();
        }

        return false;
    }

    protected virtual void FormFinished()
    {
        // this should be implemented on viewControllers using "EnableNextKeyForTextFields"
    }

Then, in your view implementation, you just need to pass all your textfields in a single array:

EnableNextKeyForTextFields(new UITextField[] { NameField, SurnameField, EmailField, PasswordField });

When the users clicks "return" when editing the last field of the array, the method "FormFinished" will be called, so you can override it in your implementation:

protected override void FormFinished()
{
    (ViewModel as RegisterViewModel).Register(); // or whatever you need to do here
}

I hope this is useful for somebody


You could make use of these functions

http://iosapi.xamarin.com/?link=M%3aMonoTouch.UIKit.UIResponder.BecomeFirstResponder

BecomeFirstResponder()

and

http://iosapi.xamarin.com/?link=M%3aMonoTouch.UIKit.UIResponder.ResignFirstResponder

ResignFirstResponder()

According to the documentation you should add the textField Delegate method like this

public virtual bool ShouldReturn (UITextField textField)
{

   if (txtUsername.tag == 1) {

    UITextField txtPassword = (UITextField)this.view.ViewWithTag(2);

    txtPassword.BecomeFirstResponder();
   }
   else {
    txtUsername.ResignFirstResponder();
       }
return true;
}

Tags:

Ios

Xamarin