按下按钮时以编程方式快速关闭键盘? [英] Dismissing the keyboard when a button is pressed, programmatically with swift?
问题描述
当以编程方式快速按下按钮时,如何关闭键盘?
How can I dismiss the keyboard when a button is pressed, programmatically with swift?
推荐答案
这完全取决于触发键盘首先出现的原因.如果页面上显示了UITextField
,可以调用textField.resignFirstResponder()
再次隐藏键盘.
It all depends on what triggered the keyboard to appear in the first place. If you have a UITextField
on your page that brought it up, you can call textField.resignFirstResponder()
to hide the keyboard again.
如果您使用其他对象来抬起键盘,只需引用您使用过的任何东西,然后在该对象上调用resignFirstResponder()
.
If you used some other object to bring up the keyboard, simply take the reference of whatever you used and call resignFirstResponder()
on that object.
示例:
假设您正在使用按钮button1
关闭键盘,并且您有textField1
会触发键盘.
Lets say you are using a button button1
to close the keyboard, and you have a textField1
that is triggering the keyboard.
button1.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "buttonTapped"))
然后在您的buttonTappedFunction
func buttonTapped(){
textField1.resignFirstResponder()
}
这篇关于按下按钮时以编程方式快速关闭键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!