当按下按钮时,用swift编程关闭键盘?

时间:2023-01-23 14:50:40

How can I dismiss the keyboard when a button is pressed, programmatically with swift?

当按下按钮时,如何以编程方式用swift关闭键盘?

4 个解决方案

#1


8  

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.

这一切都取决于首先是什么触发了键盘。如果你的页面上有一个UITextField,你可以调用textField.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.

如果您使用其他对象来打开键盘,只需获取您使用的任何对象的引用,并在该对象上调用resignFirstResponder()。

Example:

例子:

Lets say you are using a button button1 to close the keyboard, and you have a textField1 that is triggering the keyboard.

假设您正在使用按钮按钮按钮1来关闭键盘,您有一个触发键盘的textField1。

button1.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "buttonTapped"))

Then in your buttonTappedFunction

然后在你buttonTappedFunction

func buttonTapped(){
  textField1.resignFirstResponder()
}

#2


11  

There is also the so called "big hammer".

还有所谓的“大锤”。

Just call self.view.endEditing(true)

就叫self.view.endEditing(真正的)

The documentation states the following for this method:

文件说明了这种方法的下列内容:

Causes the view (or one of its embedded text fields) to resign the first responder status.

使视图(或其中一个嵌入的文本字段)放弃first responder状态。

#3


2  

Setting the UITextfield delegate programmatically swift 2.2 Add UITextFieldDelegate in your class.

在类中以编程方式设置UITextfield委托swift 2.2添加UITextFieldDelegate。

https://*.com/a/31689406/5576747

https://*.com/a/31689406/5576747

#4


1  

you have to call resignFirstResponder on currently active UI Element (most likely, UITextField).

您必须在当前活动的UI元素(很可能是UITextField)上调用resignFirstResponder。

#1


8  

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.

这一切都取决于首先是什么触发了键盘。如果你的页面上有一个UITextField,你可以调用textField.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.

如果您使用其他对象来打开键盘,只需获取您使用的任何对象的引用,并在该对象上调用resignFirstResponder()。

Example:

例子:

Lets say you are using a button button1 to close the keyboard, and you have a textField1 that is triggering the keyboard.

假设您正在使用按钮按钮按钮1来关闭键盘,您有一个触发键盘的textField1。

button1.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "buttonTapped"))

Then in your buttonTappedFunction

然后在你buttonTappedFunction

func buttonTapped(){
  textField1.resignFirstResponder()
}

#2


11  

There is also the so called "big hammer".

还有所谓的“大锤”。

Just call self.view.endEditing(true)

就叫self.view.endEditing(真正的)

The documentation states the following for this method:

文件说明了这种方法的下列内容:

Causes the view (or one of its embedded text fields) to resign the first responder status.

使视图(或其中一个嵌入的文本字段)放弃first responder状态。

#3


2  

Setting the UITextfield delegate programmatically swift 2.2 Add UITextFieldDelegate in your class.

在类中以编程方式设置UITextfield委托swift 2.2添加UITextFieldDelegate。

https://*.com/a/31689406/5576747

https://*.com/a/31689406/5576747

#4


1  

you have to call resignFirstResponder on currently active UI Element (most likely, UITextField).

您必须在当前活动的UI元素(很可能是UITextField)上调用resignFirstResponder。