0

我在静态 tableviewcontroller 中有几个 UITextFields。我为每个文本字段指定了一个标签值,这样当用户在键盘上单击下一步时,我可以获得带有下一个标签的文本字段并调用 becomeFirstResponder 以便用户在文本字段之间导航。

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    let tag = textField.tag
    if let tf = self.view.viewWithTag(tag + 1) as? UITextField {
         tf.becomeFirstResponder()
     }
}

这基本上会奏效。但是,当我使用 ReactiveKit/Bond 时,特别是当我在 viewdidload 中调用以下行(假设 lastName 是下一个文本字段)以将 UI 与模型绑定时:

profile.lastName.bidirectionalBind(to: lastName.reactive.text)

下一个文本字段(lastName)将进入编辑模式几毫秒,然后键盘被关闭,文本字段不再处于编辑模式。

当我注释掉粘合线时,逻辑就会成功。我曾尝试用单向键替换双向键或调用 obserNext 但这也会导致同样的问题。

4

2 回答 2

1

经过一段时间的搜索、调试和玩耍,我将调用 becomeFirstResponder 的行更改为异步调度,具有讽刺意味的是,这使其工作。然而,我不知道为什么。我在这里的答案中找到了这个解决方案(对于不同的反应库)。

更新代码:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
   let tag = textField.tag
    if let tf = self.view.viewWithTag(tag + 1) as? UITextField {
            DispatchQueue.main.async {
                tf.becomeFirstResponder()
            }
        }
}
于 2018-02-09T12:18:45.190 回答
0
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if let tf = self.view.viewWithTag(textField.tag + 1) as? UITextField {
        tf.becomeFirstResponder()
        return false
    }
    return true
}

试试这个代码。您不需要显式调用resignFirstResponder()当前文本字段。打电话becomeFirstResponder()应该就够了。

于 2018-02-08T20:09:57.133 回答