全員にファーストレスポンダーを辞任したいと思いますuitextfield
。uitextfields
配列に配置することでこれを完了することができますが、配列の使用を避けたかったです。辞任は、すべてのタイプのuiTextFieldで発生する必要があります。
これはうまくいきます
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var text: UITextField!
@IBOutlet weak var textFieldtwo: UITextField!
var textField = [UITextField]()
override func viewDidLoad() {
super.viewDidLoad()
self.textField = [text,textFieldtwo]
for item in textField {
item.delegate = self
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("text")
for item in textField {
item.resignFirstResponder()
}
}
}
あなたはこれを試すことができます:
for textField in self.view.subviews where textField is UITextField {
textField.resignFirstResponder()
}
ただし、を使用せずにを押すか、画面上の任意の場所をタップしてキーボードを閉じたい場合return
keyboard
touchesBegan
これで試すことができます:
// For pressing return on the keyboard to dismiss keyboard
func textFieldShouldReturn(textField: UITextField) -> Bool {
for textField in self.view.subviews where textField is UITextField {
textField.resignFirstResponder()
}
return true
}
...
func hideKeyboard() {
view.endEditing(true)
}
そしてこれをあなたに追加してくださいviewDidLoad
:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
view.addGestureRecognizer(tapGesture)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加