在我的自定义UITextField
中,textFieldDidBeginEditing()
和textFieldDidEndEditing()
委托方法被调用,但是
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
@IBDesignable class CustomUITextField: UITextField, UITextFieldDelegate{
//getting Called
@objc func textFieldDidBeginEditing() {
}
//getting Called
@objc func textFieldDidEndEditing(){
}
//Not getting Called
@objc func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
}
}
我不知道为什么没有接到电话。为此需要做任何额外的工作吗?
谢谢。
答案 0 :(得分:0)
您需要设置代表
@IBDesignable class CustomUITextField: UITextField, UITextFieldDelegate{
override init(frame: CGRect) {
super.init(frame: frame)
self.delegate = self
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = self
}
//getting Called
@objc func textFieldDidBeginEditing() {
print("textFieldDidBeginEditing")
}
//getting Called
@objc func textFieldDidEndEditing(){
print("textFieldDidEndEditing")
}
//Not getting Called
@objc func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print("shouldChangeCharactersIn")
return true
}
}