iOS app "next" key won't go to the next text field

48,245

Solution 1

The return key doesn't do anything special in a text field by default. You need to explicitly change the first responder:

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == self.textPassword) {
        [theTextField resignFirstResponder];
    } else if (theTextField == self.textUsername) {
        [self.textPassword becomeFirstResponder];
    }
    return YES;
}

Solution 2

I use this code that allows me to control the form behavior in the storyboard:

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    if(textField.returnKeyType==UIReturnKeyNext) {
        UIView *next = [[textField superview] viewWithTag:textField.tag+1];
        [next becomeFirstResponder];
    } else if (textField.returnKeyType==UIReturnKeyDone) {
        [textField resignFirstResponder];
    }
    return YES;
} 

All you need is to assign the order value as tag, and returnkey according to Next or Done on each input control.

Solution 3

for those who want to use Swift language:

class MyClass: UIViewController, UITextFieldDelegate {

@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
@IBOutlet weak var text3: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    text1.delegate = self
    text2.delegate = self
    text3.delegate = self


}

//....

func textFieldShouldReturn(textField: UITextField) -> Bool{
    if textField == self.text1 {
        self.text2.becomeFirstResponder()
    }else if textField == self.text2{
        self.text3.becomeFirstResponder()
    }else{
        self.text1.becomeFirstResponder()
    }
    return true
 }

 //...

}

:-)

Solution 4

You have to add the UITextFieldDelegate in the header-file. Then you have to set

theTextField.delegate = self;

in the viewDidLoad-method. After that you can go on with

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == self.textPassword) {
        [theTextField resignFirstResponder];
    } else if (theTextField == self.textUsername) {
        [self.textPassword becomeFirstResponder];
    }
    return YES;
}

Solution 5

Many solutions to this problem exist. See the responses posted to this question: How to navigate through textfields (Next / Done Buttons).

Read more in the UIResponder Class Reference.

Share:
48,245
BamBamBeano
Author by

BamBamBeano

Updated on July 09, 2022

Comments

  • BamBamBeano
    BamBamBeano almost 2 years

    I have a simple scene (using storyboard in IB) with a Username and Password text box. I've set the keyboard to close when you are on the Password text field but can't get the next(return) button to work on the Username to switch the focus (or First Responder) to the Password text box.

    I'm closing the keyboard while on the Password text field like this:

    - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == self.textPassword) {
        [theTextField resignFirstResponder];
    }
    return YES;
    }
    

    I know it is something similar to this but just can't nail it down.

  • vinay chorpa
    vinay chorpa almost 10 years
    Best answer among all !!
  • setherj
    setherj almost 10 years
    So how would you set tags for the text fields?
  • setherj
    setherj almost 10 years
    Is it as simple as exampleField.tag = 1; ?
  • Esteve
    Esteve almost 10 years
    Yes, just set tag property. Use tags to keep editing order, and if you use storyboards is still easier.
  • Uday
    Uday over 9 years
    Can you share the same for swift?
  • Stefan
    Stefan over 8 years
    This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review
  • Ginés SM
    Ginés SM over 8 years
    if UserName has tag set to 1 and return key 'UIReturnKeyNext', and Password has tag set to 2 and return key to 'UIReturnKeySend' or 'UIReturnKeyDone', the code solves the problem, at least in a xib file. Why does not this answer the question? My textfields are: - Name - Surname - Email - Password -Re-type Password. and the codes works perfectly.
  • vrwim
    vrwim about 8 years
    @RaphaelRoyer-Rivard you should set the delegate of the UITextField to the viewcontroller (or whatever class this code is added to)
  • woody121
    woody121 over 7 years
    @vrwin's comment should be part of the answer, this is step 1 to this answer working.
  • Dhiru
    Dhiru over 7 years
    This also should be accepted as answer ,,,,,, best ways to navigate through UITextFiled
  • Dany Balian
    Dany Balian about 4 years
    Brilliant idea!