新手來ReactiveCocoa和ReactiveSwfit在這裏...很抱歉,如果答案是明顯的。使用RAC(5)解除UITextField中的鍵盤?
我正在嘗試將Start Developing iOS Apps with Swift示例改編爲ReactiveSwift/ReactiveCocoa,並且我遇到了「翻譯」UITextField
的Delegate方法的問題 - 該方法擺脫了鍵盤並基本結束了編輯(所以我可以捕捉到mealNameLabel
文本字段):
- FUNC textFieldShouldReturn(_文本框:的UITextField) - >布爾
我使用
nameTextField.reactive.textValues.observeValues { value in
viewModel.mealName.swap(value ?? "")
}
// Setup bindings to update the view's meal label
// based on data from the View Model
mealNameLabel.reactive.text <~ viewModel.mealLabel
得到從文本字段中的值到視圖模型和滲透視圖模型的標籤回的UILabel(曲...)
這工作得很好,只要我保持viewController
作爲UITextField
我的代表仍然執行本教程中描述的方法,並在上面提到。本質:
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.delegate = self
// view controller logic
...
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Hide the keyboard.
textField.resignFirstResponder()
return true
}
我嘗試使用
nameTextField.reactive.controlEvents
但慘敗由於我缺乏瞭解controlEvents
失敗(文檔的任何地方?)。
那麼當用戶完成編輯時,我需要做些什麼來使鍵盤消失,即「被動方式」?
謝謝!
我會使用'editingDidEnd'而不是'primaryActionTriggered'。 –
@ SebastianHojas:我最初嘗試使用.editingDidEnd,但似乎並不奏效(兩者都在模擬器和設備上)。通過觀察那個事件,我可以看到它在鍵盤被解散之後觸發(奇怪?)。 – Nick