开始用如下代理方法发现只能监听到变化后的前一位
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{//变化后的字符串NSLog(@"%@",textField.text);return YES;}
然后在代理方法里面用如下方法可获取到变化后的完整内容
NSString * new_text_str = [textField.text stringByReplacingCharactersInRange:range withString:string];
但是发现只有文本内容增加时可以监听到,当点击键盘上删除按钮文本内容减少时是监听不到的,最后用下面通知的方法可以完美解决
- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldTextDidChangeOneCI:)name:UITextFieldTextDidChangeNotificationobject:_textField2];}-(void)textFieldTextDidChangeOneCI:(NSNotification *)notification{UITextField *textfield=[notification object];NSLog(@"ssssss %@",textfield.text);}
发现有更简单的方法如下
//添加方法 [textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];- (void)textFieldChanged:(UITextField*)textField{NSString *_string = textField.text;}