Xamarin iOS - move to the next text field with a return key

I could not find anything in the Xamarin documentation about moving to the next text field in a series of text fields in a form, just a little keyboard removal guide.

To keep things simple, I use the txtUsername (tag 1) text box and the txtPassword (tag 2) text box. When I implement the following code, it is not ported to Xamarin Studio. Does anyone know how to do this code in Xamarin Studio, or alternatively if I can pass 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

+7
ios xamarin
source share
5 answers

You can use 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 a 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; } 
+2
source share

I think the easiest way to do this is to use the ShouldReturn method on your UITextFields using the BecomeFirstResponder method. For example, for the login form with the username and password UITextFields, as follows (in the 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 the "Next" button, when the username is active, it goes into the "Password" field. When you click the Go button, a form is displayed in the password field.

+6
source share

Here is an example of how to implement in Xamarin.iOS. There may be other simpler ways to do this, but I could not find them, and this works for me.

I subclassed UITextFieldDelegate to add a special event handler, then instantiated it in my view controller code, assigned a lambda expression to a custom event handler, to access the instance variables and set this delegate instance as a delegate to my 2 ext fields:

  public class LoginTextFieldDelegate : UITextFieldDelegate { public EventHandler<UITextField> OnShouldReturn; public override bool ShouldReturn(UITextField textField) { if (OnShouldReturn != null) { OnShouldReturn(this, textField); } return true; } } 

Then in my view the controller's ViewDidLoad method:

  var textDelegate = new LoginTextFieldDelegate { OnShouldReturn = (sender, textField) => { if (textField.Tag == txtEmailAddress.Tag) { txtPassword.BecomeFirstResponder(); } else if (textField.Tag == txtPassword.Tag) { txtPassword.ResignFirstResponder(); } } }; txtEmailAddress.Delegate = textDelegate; txtPassword.Delegate = textDelegate; 

Do not forget to set a separate tag on each UITextField in the code / interface builder!

+2
source share

I wrote a general solution for each form (not only those that have 2 fields).

I have 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 opinion, you just need to pass all your text fields in one array:

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

When users press "return" while editing the last field of the array, the "FormFinished" method is called, so you can override it in your implementation:

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

Hope this is helpful to someone.

+2
source share

It may be easier to use an available class here

 public override void ViewDidLoad () { base.ViewDidLoad (); // Manage keyboard and tab order new [] { usernameField, // Tab order = 0 passwordField, // Tab order = 1 emailField // Tab order = 2 }.InitializeNavigationOnFields(); } 

Just call the method that passes all the text fields in the order you want them to be moved from the keyboard.

0
source share

All Articles