iOS 应用程序“下一步”键不会转到下一个文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9540500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:08:51  来源:igfitidea点击:

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

iphoneobjective-ciosxcodefirst-responder

提问by BamBamBeano

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.

我有一个带有用户名和密码文本框的简单场景(在 IB 中使用故事板)。当您在密码文本字段上时,我已将键盘设置为关闭,但无法使用下一个(返回)按钮处理用户名以将焦点(或第一响应者)切换到密码文本框。

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.

我知道这与此类似,但无法确定。

回答by rob mayoff

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

回答by Esteve

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 returnkeyaccording to Nextor Doneon each input control.

您所需要的只是将 order 值分配为tag,并returnkey根据每个输入控件上的NextDone

回答by N3tMaster

for those who want to use Swift language:

对于那些想要使用 Swift 语言的人:

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
 }

 //...

}

}

:-)

:-)

回答by FTFT1234

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

您必须在头文件中添加 UITextFieldDelegate。然后你必须设置

theTextField.delegate = self;

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

在 viewDidLoad 方法中。之后你可以继续

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

回答by sherbondy

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

这个问题有很多解决方案。请参阅针对此问题发布的回复:How to navigation through textfields (Next / Done Buttons)

Read more in the UIResponder Class Reference.

UIResponder 类参考中阅读更多内容。

回答by bryanjclark

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

回答by 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

您可以使用 UITextfield 的这个子类,根据文本/字符串条件动态更改 UIReturnKey,然后在 textFieldShouldReturn:(UITextField *)textField 委托中执行继续操作

if (textField.returnKeyType == UIReturnKeyNext)

https://github.com/codeinteractiveapps/OBReturnKeyTextField

回答by 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.

我遇到了密码文本字段获得焦点然后立即失去焦点的问题。最后我发现我们最好在函数 textFieldShouldReturn: 中返回 NO。如果我们返回 YES,文本字段将获得一个 insertText: 调用。那么它就会失去焦点。不知道原因。

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

我为此问题创建了一个新问题:UITextField 获得焦点,然后由于在 textFieldShouldReturn 中返回 YES 而立即失去焦点

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

回答by egeelabs

Here is a Swift 2.0 Version of Rob Mayoff

这是 Rob Mayoff 的 Swift 2.0 版本

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

回答by Júlia Garrigós

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