Swift UITextFieldShouldReturn返回键点击 [英] Swift UITextFieldShouldReturn Return Key Tap
问题描述
(iOS8,Xcode6,Swift)
使用Swift,如何在返回按钮上点击?
(iOS8, Xcode6, Swift) Using Swift, how do I capture a tap on the "Return" button?
以下文档link使用 textFieldShouldReturn
方法指定:
The doc at the following link specifies using the textFieldShouldReturn
method:
// Swift
@optional func textFieldShouldReturn(_ textField: UITextField!) -> Bool
我挂断的地方是_ textField部分。我使用Storyboard创建了文本字段。如何捕获此特定文本字段的通知?我是否需要创建一个新类并将其设置为此文本字段的委托?我是否为文件分配了一个名称,然后以某种方式挂钩?
Where I'm hung up is in the "_ textField" part. I've created the text field using Storyboard. How do I capture notifications for this specific text field? Do I need to create a new class and set it as a delegate for this text field? Do I assign the text filed a name, and then somehow hook into it?
https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn
推荐答案
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class
@IBOutlet var txtValue: UITextField //create a textfile variable
override func viewDidLoad() {
super.viewDidLoad()
txtValue.delegate = self //set delegate to textfile
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
这篇关于Swift UITextFieldShouldReturn返回键点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!