Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get keyboard to move to next textfield

I have several UITextFields that are in a group... I need to be able to move from the first to the last, and on the last make it FirstResponder.

I thought the Next key would move the focus, and Done would cause it to become FirstResponder, but apparently not.

Ideas?

like image 585
SpokaneDude Avatar asked Jan 21 '11 15:01

SpokaneDude


2 Answers

The "Accepted Answer" is right, but I thought a little code wouldn't hurt for people who are new to MonoTouch.

public override void ViewDidLoad () {
    base.ViewDidLoad ();

    // make first text field have focus
    this.txtMake.BecomeFirstResponder();

    // make 'return' shift focus to next text field
    this.txtMake.ShouldReturn = delegate(UITextField textField) {
        this.txtModel.BecomeFirstResponder ();
        return true;
    };

    // make 'return' on last text field save and close the form
    this.txtModel.ShouldReturn = delegate(UITextField textField) {
        this.txtModel.ResignFirstResponder();
        this.SaveAndClose ();
        return true;
    };
}

private void SaveAndClose() {
    // todo: do something with your data

    // navigate back
    this.NavigationController.PopViewControllerAnimated(true);
}

Hope this helps others.

like image 148
Rodney Willis Avatar answered Nov 10 '22 11:11

Rodney Willis


You have to implement the UITextFieldDelegate's ShouldReturn method and in there make the next text field BecomeFirstResponder(). Then return/next will take you to the next field. If return is hit on the last textfield you will call ResignFirstResponder() on that field.

like image 28
Krumelur Avatar answered Nov 10 '22 11:11

Krumelur