42

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.

1

11 Answers 11

91

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;
}
3
  • Can you share the same for swift?
    – Uday
    Commented Jan 12, 2015 at 7:01
  • 2
    @RaphaelRoyer-Rivard you should set the delegate of the UITextField to the viewcontroller (or whatever class this code is added to)
    – vrwim
    Commented Apr 1, 2016 at 11:46
  • @vrwin's comment should be part of the answer, this is step 1 to this answer working.
    – woody121
    Commented Dec 8, 2016 at 4:48
42

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.

6
  • 3
    Best answer among all !! Commented Jul 3, 2014 at 6:13
  • So how would you set tags for the text fields?
    – setherj
    Commented Sep 23, 2014 at 23:23
  • 2
    Is it as simple as exampleField.tag = 1; ?
    – setherj
    Commented Sep 23, 2014 at 23:39
  • Yes, just set tag property. Use tags to keep editing order, and if you use storyboards is still easier.
    – Esteve
    Commented Sep 24, 2014 at 8:46
  • This also should be accepted as answer ,,,,,, best ways to navigate through UITextFiled
    – Dhiru
    Commented Jan 20, 2017 at 6:34
11

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
 }

 //...

}

:-)

9

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;
}
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.

3

Here's what you would put:

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

You can use this subclass of UITextfield that dynamically change the UIReturnKey according to text/string condition, and then perform your continuation action in the textFieldShouldReturn:(UITextField *)textField delegate

if (textField.returnKeyType == UIReturnKeyNext)

https://github.com/codeinteractiveapps/OBReturnKeyTextField
0

I got an issue that password textfield get focus and then lose focus immediately. At last I found that we'd better return NO in function textFieldShouldReturn:. If we return YES, the text field will get a insertText: call. Then it will lose focus. Don't know the reason.

I have created a new question for this issue: UITextField get focus and then lose focus immediately due to return YES in textFieldShouldReturn

If anyone knows the reason, please let me know. Thanks.

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    if (theTextField == self.textPassword) {
        [theTextField resignFirstResponder];
    } else if (theTextField == self.textUsername) {
        [self.textPassword becomeFirstResponder];
    }
    // we'd better return NO here
    return NO;
}
0

Here is a Swift 2.0 Version of Rob Mayoff

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField == self.emailTextField {
        self.passwordTextField.becomeFirstResponder()
    }else{
        self.passwordTextField.resignFirstResponder()
    }
    return false
}
0

The swift version could be:

extension LoginViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField == self.emailTextField {
            passwordTextField.becomeFirstResponder()
        } else if textField == self.passwordTextField {
            passwordTextField.resignFirstResponder()
        }
        return true
    }
}
-1

I tried to use Esteve's answer but it doesn't work. Maybe because I'm using xib files. What I've done is change his code to make it works in my code.

It works in the same way. All you need is to assign the order value as tag, and return key according to Next or Done or Send on each input control.

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    if(textField.returnKeyType==UIReturnKeyNext) {

        UITextField *next = (UITextField *)[self.view viewWithTag:textField.tag+1];
        [next becomeFirstResponder];

    } else if (textField.returnKeyType==UIReturnKeySend) {

        //Do whatever you want with the last TextField
    }

    return YES;
}
2
  • 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
    – Stefan
    Commented Feb 3, 2016 at 17:15
  • 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.
    – Ginés SM
    Commented Feb 3, 2016 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.