ios 按下返回键时如何隐藏键盘 - Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41537721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide keyboard when return key is hit - Swift
提问by stevetheipad
Trying to hide the iOS keyboard when the return key is hit, but instead it halts and gives me the error seen in the image. Here's the code I'm using:
尝试在按下返回键时隐藏 iOS 键盘,但它会停止并显示图像中的错误。这是我正在使用的代码:
@IBOutlet weak var scoreText: UITextField!
func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
回答by Mr. Xcoder
Your problem is that you didn't delegate a textField in order to use that method. First, your class must include the UITextFieldDelegate
protocol:
您的问题是您没有委托 textField 来使用该方法。首先,您的类必须包含UITextFieldDelegate
协议:
class yourClass: UIViewController, UITextFieldDelegate { ... }
And in the viewDidLoad()
method, add this as well:
在viewDidLoad()
方法中,也添加这个:
scoreText.delegate = self
And then you have to change this:
然后你必须改变这个:
func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
to this:
对此:
func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
Final code:
最终代码:
class yourClass: UIViewController, UITextFieldDelegate {
@IBOutlet weak var scoreText: UITextField!
override func viewDidLoad(){
super.viewDidLoad()
scoreText.delegate = self
}
func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
self.view.endEditing()
return true
}
}
If this is not working, the problem is not the textFieldShouldReturn()
function. Please check your outlet connections.
如果这不起作用,则问题不在于textFieldShouldReturn()
功能。请检查您的插座连接。
回答by Mochi
Try this
尝试这个
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
回答by Enamul Haque
In swift 4 or 5 , You can use like :
在 swift 4 或 5 中,您可以使用像:
- create a project
- add UITextField & connect to ViewController
- implement UITextFieldDelegate to ViewController
- Initialize the UITextField delegate
- Add textFieldShouldReturn method in ViewController
Here is full code
class ViewController: UIViewController,UITextFieldDelegate { //Connect to text field @IBOutlet weak var scoreText: UITextField! override func viewDidLoad() { super.viewDidLoad() //must initialize delegate scoreText.delegate = self } //add method func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } }
- 创建项目
- 添加 UITextField 并连接到 ViewController
- 将 UITextFieldDelegate 实现到 ViewController
- 初始化 UITextField 委托
- 在 ViewController 中添加 textFieldShouldReturn 方法
这是完整的代码
class ViewController: UIViewController,UITextFieldDelegate { //Connect to text field @IBOutlet weak var scoreText: UITextField! override func viewDidLoad() { super.viewDidLoad() //must initialize delegate scoreText.delegate = self } //add method func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } }