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

2019-01-21 07:30发布

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.

10条回答
不美不萌又怎样
2楼-- · 2019-01-21 08:06

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
查看更多
混吃等死
3楼-- · 2019-01-21 08:07

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.

查看更多
4楼-- · 2019-01-21 08:07

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;
}
查看更多
再贱就再见
5楼-- · 2019-01-21 08:19

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
}
查看更多
够拽才男人
6楼-- · 2019-01-21 08:19

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;
}
查看更多
Summer. ? 凉城
7楼-- · 2019-01-21 08:25

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;
}
查看更多
登录 后发表回答