我想知道您如何允许通过按软键盘上的返回键或点击 UIButton 来执行操作。
UI 按钮已设置为执行 IBAction。
如何让用户也可以按键盘上的返回键来执行相同的操作?
最佳答案
确保你的类扩展了 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
}
关于xcode - 如何让返回键执行与 Swift 中按下按钮相同的操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26288124/