UITextfield文本变化,代理方法

1.实时监视文本变化
[textfield addTarget:self action:@selector(changed:) forControlEvents:UIControlEventEditingChanged];
//添加通知
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textDidChange:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];

2.代理方法
//首次点击textField时调用,return no时不会输出
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField        // return NO to disallow editing.
{
    NSLog(@"---%@", textField.text);
    return YES;
}
//首次点击textField时调用
- (void)textFieldDidBeginEditing:(UITextField *)textField           // became first responder
{
    NSLog(@"===%@", textField.text);
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string   // return NO to not change text
{
    //return no时输入内容无法改变
    //string:每次输入的新字符串
    //range:range.location:新输入字符串的位置, range.length:0
    NSLog(@"%lu, %lu", (unsigned long)range.location,(unsigned long)range.length);
    NSLog(@"%@", string);
    return YES;
}


你可能感兴趣的:(UITextfield文本变化,代理方法)