当我们采用联想的方式输入内容时,会发现并不会走以下方法,哪该如何去获取当前联想到的字符呢?
- (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.
}
本文介绍如何在 iOS 应用中捕获 UITextField 控件的联想输入字符。通过监听 UITextField 的 UITextFieldTextDidChangeNotification 通知,可以在 textField 文本发生变化时获取到当前的文本内容。
1659

被折叠的 条评论
为什么被折叠?



