0

我正在尝试在输入 UITextField 时获取字符数。我使用 UIControlEventEditingChanged 通知来获取当前计数:

[textField addTarget:self action:@selector(handleTextDidChange:) forControlEvents:UIControlEventEditingChanged];

每当我在该字段中输入任何新字符时,该代码都可以正常工作并触发。每当我删除任何字符时,它也会被触发,除非它是文本字段中唯一剩余的字符。

任何想法为什么?

4

1 回答 1

0

I tested your code and it was working fine for me in iOS 7.1. What you could try is:

Make your ViewController implement

<UITextFieldDelegate> 

and set

textfield.delegate = self;

And implement the following function, which also always get called, whenever a character should be changed (even the last one). Make sure to always return YES if the character should be changed. With the range and textfield.text you can always access the current character to be changed:

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

    return YES;

}
于 2014-09-08T19:21:12.317 回答