当我使用ReactiveKit / Bond时,当我调用becomeFirstResponder后,UITextField立即重新启动第一个响应者

时间:2018-02-08 19:49:56

标签: ios swift uitextfield uitextfielddelegate reactivekit

我在静态tableviewcontroller中有几个UITextField。我为每个文本字段指定了一个标记值,这样当用户在键盘上单击下一步时,我可以获得带有下一个标记的文本字段并调用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,但这也会导致同样的问题。

2 个答案:

答案 0 :(得分:1)

经过一段时间的搜索,调试和游戏,我已经改变了调用yesFirstResponder的行,这是异步调度的,具有讽刺意味的是,它使它工作。然而,我不知道为什么。我在答案here中找到了这个解决方案(对于不同的反应库)。

更新代码:

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

答案 1 :(得分:0)

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

试试这段代码。您无需在当前textField上显式调用resignFirstResponder()。调用becomeFirstResponder()就足够了。