【发布时间】:2015-10-19 17:45:06
【问题描述】:
我是一名使用 Swift 的初学者程序员。我一直在做一个待办事项列表应用程序。我试图通过返回键关闭键盘。我尝试了“self.view.endEditing(true)”和“resignFirstResponder()”方法,但它们都不起作用。这是我的代码:(我正在使用选项卡应用程序)
class SecondViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var text: UITextField!
@IBAction func addItem(sender: AnyObject) {
toDoList.append(text.text)
text.text = ""
self.view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true) // This works fine here
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true) // This is not working...
return true
}
}
在最后一个函数中,我也尝试过使用以下内容:
func textFieldShouldReturn(textField: UITextField) -> Bool {
text.resignFirstResponder() // This is not working...
return true
}
但由于某种原因,没有一个人能按我的意愿工作。当我在“touchesBegan”方法中使用它时,它工作正常。你能告诉我我在我的代码中犯了什么错误吗?谢谢。
【问题讨论】:
-
你设置了 UITextField 的代理了吗?
-
您没有覆盖 textField... 函数。我敢打赌那是你的问题。
-
是的。那是我做错了。谢谢大家
标签: ios swift keyboard xcode6 dismiss