簡體   English   中英

當我使用ReactiveKit / Bond調用成為firstFirstResponder之后,UITextField會立即退出第一響應者

[英]UITextField resigns first responder right after I call becomeFirstResponder when I use ReactiveKit/Bond

我在靜態tableviewcontroller中有幾個UITextFields。 我為每個文本字段指定了一個標簽值,以便當用戶在鍵盤上單擊“下一步”時,我可以獲取帶有下一個標簽的文本字段,然后調用“ firstFirstResponder”,以便用戶在文本字段之間導航。

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)將進入編輯模式幾毫秒,然后關閉鍵盤,並且該文本字段不再處於編輯模式。

當我注釋掉粘合線時,邏輯將成功。 我嘗試用1向鍵替換雙向鍵或調用obserNext,但這也會導致相同的問題。

經過一段時間的搜索,調試和調試之后,我更改了成為異步地分派的調用變為FirstFirstResponder的行,這使其可以工作。 但是,我不知道為什么。 我在這里的答案中找到了該解決方案(針對其他反應庫)。

更新的代碼:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
   let tag = textField.tag
    if let tf = self.view.viewWithTag(tag + 1) as? UITextField {
            DispatchQueue.main.async {
                tf.becomeFirstResponder()
            }
        }
}
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()應該足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM