我正在学习Xcode和Swift,所以我决定阅读Apple Developer网站上的教程。我已将我的文本字段设置为关闭 如本教程所述,键盘,而不是消除键盘 键盘并执行
完成的操作func textFieldDidEndEditing(_ textField: UITextField)
文本字段仅换行,而键盘不关闭。
在键入并按完成之前:
键入并按Enter键后:
这是我的代码:
答案 0 :(得分:0)
尝试一下:
class MyViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var textField : UITextField
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.textField.delegate = self
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
}
答案 1 :(得分:0)
确保它是一个UITextField而不是UITextView
答案 2 :(得分:0)
尝试:
class ViewController: UIViewController , UITextFieldDelegate {
@IBOutlet weak var textFieldTXT: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textFieldTXT.delegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textFieldTXT.resignFirstResponder()
return true
}
}