你知道吗(你能看到)为什么我不能从FirstResponder辞职并隐藏键盘?
仅供参考:Xcode 9
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var text: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.text.delegate = self
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}
var keyboardInputEnabled = true
@objc func tapFunction(sender:UITapGestureRecognizer) {
if self.keyboardInputEnabled {
self.text.becomeFirstResponder()
}else{
self.view.endEditing(true)
self.text.resignFirstResponder()
}
self.keyboardInputEnabled = !self.keyboardInputEnabled
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return self.keyboardInputEnabled
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return self.keyboardInputEnabled
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return self.keyboardInputEnabled
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
答案 0 :(得分:2)
因为textFieldShouldEndEditing
第二次运行tapFunction
会导致render() {
const description = JSON.parse(rowData.description);
const lang = I18n.locale;
return (
<Cell cellTitle='test' cellDescription={description[lang]} ... />
)
}
返回false,从而阻止它结束编辑模式。
取自this
通常,您将从此方法返回true以允许文本 字段以辞退第一响应者状态。你可能会返回false, 但是,如果您的代理人检测到无效内容 文本域。返回false会阻止用户切换到 另一个控件,直到文本字段包含有效值。
答案 1 :(得分:0)
尝试
self.view.endEditing(true)
用于禁用键盘