【发布时间】:2016-10-16 13:00:12
【问题描述】:
我尝试在ViewController 中实现UITextFieldDelegate 协议。我从apple tutorial 开始。我实现了相同的方法,例如在教程中,但它不起作用。 (XCODE 8)
class ViewController: UIViewController, UITextFieldDelegate {
// MARK: Properties
@IBOutlet weak var recipeNameField: UITextField!
@IBOutlet weak var recipeNameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
recipeNameField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: Actions
@IBAction func onSetDefaultRecipeClick(_ sender: UIButton) {
recipeNameField.text = "Deafult recipe name"
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
print("return")
return true
}
}
【问题讨论】:
-
从键盘打印时是否将“return”打印到控制台?
-
不。什么都没有发生
-
好的,
viewDidLoad被调用了吗?您可以使用断点或另一个print("view did load")检查它 -
是的,它被调用了。奇怪...
标签: ios swift3 uitextfielddelegate