原來UITextField 文字改變時會有個通知啊
阿新 • • 發佈:2019-02-01
轉載自: http://ask.csdn.net/questions/3386
用我方法:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(textFieldTextDidChangeOneCI:)
name:UITextFieldTextDidChangeNotification
object:textfield];
}
然後將你的方法改為:
-(void)textFieldTextDidChangeOneCI:(NSNotification *)notification { UITextField *textfield=[notification object]; NSLog(@"%@",textfield.text); }
本來用它的代理方法:(但發現這樣始終不行,因為self.contentField.text 這個值始終比輸入框的實際文字少一位,用上邊的方法搞定)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"字元: %@--%@--%d",self.contentField.text,string,range.location);
if (range.location == 11
[[NSNotificationCenter defaultCenter] postNotificationName:GNNotifiIsShowPassTextField object:textField.text userInfo:nil];
returnNO; // return NO to not change text
}
returnYES;
}