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

回眸只為那壹抹淺笑 提交于 2019-11-28 16:30:58

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

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.

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
 }

 //...

}

:-)

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

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.

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

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
Harrison Xi

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

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
}

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!