【发布时间】:2018-05-23 02:32:45
【问题描述】:
我有这个函数但问题是我不知道如何在super.viewDidLoad上调用这个函数
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let text = namaTextField.text {
if let floatingLabelTextField = textField as? SkyFloatingLabelTextField {
if(text.characters.count < 3 || !text.contains("@")) {
floatingLabelTextField.errorMessage = "Invalid email"
}
else {
// The error message will only disappear when we reset it to nil or empty string
floatingLabelTextField.errorMessage = ""
}
}
}
return true
}
【问题讨论】:
-
这是一个 UITextField 的委托函数,每次在 UITextField 的文本发生变化之前都会调用它,以便您确定是否应该发生变化。通常不需要显式调用它,但如果你真的这样做了,那么只需按照方法需要传入 textField、范围和替换字符串。
-
如果你遇到的问题是方法没有被调用,那么它应该仔细检查你的这个类是否是一个 UITextField 委托并且 nameTextField.delegate 设置为这个类的一个实例。跨度>
-
您认为为什么需要调用此文本字段委托方法?您不应该自己调用它。请说明你的目标。
-
您可以手动调用它(通常不会),但您必须先了解委托模式的工作原理。
标签: ios swift uitextfielddelegate