我只是试图检测用户何时开始在UITextField中输入文本。我在这里阅读了几篇文章,这些文章指示我链接UITextField,然后设置我的文本字段委托,最后实现必要的方法。我已经完成了所有这些工作,但是启动我的代码后,什么也没有显示。我们将不胜感激。
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var button: UIButton!
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
button.isEnabled = false
textField.delegate = self
}
func textFieldDidBeginEditing(textField: UITextField) {
print("TextField did begin editing method called")
}
func textFieldDidEndEditing(textField: UITextField) {
print("TextField did end editing method called\(textField.text)")
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
print("TextField should begin editing method called")
return true;
}
func textFieldShouldClear(textField: UITextField) -> Bool {
print("TextField should clear method called")
return true;
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
print("TextField should end editing method called")
return true;
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
print("While entering the characters this method gets called")
return true;
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
print("TextField should return method called")
textField.resignFirstResponder();
return true;
}
}
答案 0 :(得分:0)
正如rmaddy所述,您的方法签名错误。我认为您会收到类似“实例方法'textFieldDidEndEditing(textField :)'与协议'UITextFieldDelegate'的可选要求'textFieldDidEndEditing'几乎匹配”的警告。您应该具有以下方法:
func textFieldDidBeginEditing(_ textField: UITextField) {
print("TextField did begin editing method called")
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("TextField did end editing method called\(textField.text)")
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
print("TextField should begin editing method called")
return true;
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
print("TextField should clear method called")
return true;
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
print("TextField should end editing method called")
return true;
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print("While entering the characters this method gets called")
return true;
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
print("TextField should return method called")
textField.resignFirstResponder();
return true;
}