【问题标题】:UITextField resigns first responder right after I call becomeFirstResponder when I use ReactiveKit/Bond当我使用 ReactiveKit/Bond 时,UITextField 在我调用 becomeFirstResponder 后立即辞职第一响应者
【发布时间】:2018-07-19 12:08:04
【问题描述】:

我在静态 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 但这也会导致同样的问题。

【问题讨论】:

    标签: ios swift uitextfield uitextfielddelegate reactivekit


    【解决方案1】:

    经过一段时间的搜索、调试和玩耍后,我将调用 becomeFirstResponder 的行更改为异步调度,具有讽刺意味的是,这使它工作。然而,我不知道为什么。我在答案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()
                }
            }
    }
    

    【讨论】:

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

      试试这个代码。您不需要在当前文本字段上显式调用resignFirstResponder()。拨打becomeFirstResponder() 就足够了。

      【讨论】:

      • 我已经尝试过了,但不幸的是它不起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多