如何获得返回键以执行与Swift中的按钮按下相同的操作? [英] How do I get the return key to perform the same action as a button press in Swift?
本文介绍了如何获得返回键以执行与Swift中的按钮按下相同的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何通过按下软件键盘上的返回键或点击UIButton来允许操作。
I want to know how you allow an action to be made by either pressing the return key on the software keyboard or by tapping a UIButton.
UI按钮是已设置为执行IBAction。
The UI button is already set up to perform an IBAction.
如何让用户按键盘上的返回键执行相同的操作?
How do I also allow users to press the return key on the keyboard to perform the same action?
推荐答案
确保您的类扩展了UITextFieldDelegate协议
Make sure your class extends the UITextFieldDelegate protocol
SomeViewControllerClass : UIViewController, UITextFieldDelegate
您可以执行以下操作:
override func viewDidLoad() {
super.viewDidLoad()
self.textField.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
//textField code
textField.resignFirstResponder() //if desired
performAction()
return true
}
func performAction() {
//action events
}
这篇关于如何获得返回键以执行与Swift中的按钮按下相同的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文