当我们采用联想的方式输入内容时,会发现并不会走以下方法,哪该如何去获取当前联想到的字符呢?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return YES;
}
解决办法:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(UITextFieldTextDidChange:)
name:UITextFieldTextDidChangeNotification
object:textField];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextFieldTextDidChangeNotification
object:textField];
}
- (void) UITextFieldTextDidChange:(NSNotification*)notification
{
UITextField * textfield = (UITextField*)notification.object;
NSString * text = textfield.text;
// do something with the text and/or the text field here,
// like validation, etc.
}