可清除的 UITextField 清除文本没有触发 shouldChangeCharactersInRange

Clearable UITextField clearing text didn't trigger shouldChangeCharactersInRange

我的 ViewController 中有一个 UITextField,ViewController 符合 UITextField delegate。一切都很好。

我通过以下方式设置 UITextField 可清除模式:

[myTextField setClearButtonMode:UITextFieldViewModeWhileEditing];

问题是,当我点击myTextField的清除按钮时,无法触发委托方法shouldChangeCharactersInRange。否则,如果我输入一个字符或手动删除一个字符,它就会被触发。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
}

根据我的理解,清除文本是一个文本更改事件,我想以一种通用的方式处理这个事件,即在shouldChangeCharactersInRange中。但它就是行不通。那么,有没有什么办法可以将明文事件连接到delegate方法呢?非常感谢所有讨论。

- (BOOL)textFieldShouldClear:(UITextField *)textField

这是按下内置清除按钮时调用的方法。

有关更多帮助,请参阅文档。

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear:

:)