UITextField 监听数值变化的三种方法

本文介绍了iOS中四种监听UITextField输入变化的方法:直接监听、通过NSNotificationCenter、使用KVO及代理方式。详细展示了每种方法的实现代码及注意事项。

原文链接: http://blog.youkuaiyun.com/qxuewei/article/details/50727617

1.直接监听 - 推荐

#pragma mark - 直接添加监听方法
-(void)addTargetMethod{
    [self.textField1 addTarget:self action:@selector(textField1TextChange:) forControlEvents:UIControlEventEditingChanged];
}
-(void)textField1TextChange:(UITextField *)textField{
    NSLog(@"textField1 - 输入框内容改变,当前内容为: %@",textField.text);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.NSNotificationCenter 添加监听方法

#pragma mark - NSNotificationCenter 添加监听方法
-(void)addNSNotificationCenter{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textField2TextChange:) name:UITextFieldTextDidChangeNotification object:self.textField2];
}
-(void)textField2TextChange:(NSNotification *)noti{
    UITextField *currentTextField = (UITextField *)noti.object;
    NSLog(@"textField2 - 输入框内容改变,当前内容为: %@",currentTextField.text);
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.KVO : 注意这种方法不会直接监听键盘输入而是监听 textField.text 的改变,代码设置才会有效

-(void)addKVO{
    [self.textField3 addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    self.textField3.text = @"123";
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"text"] && object == self.textField3) {
        NSLog(@"textField3 - 输入框内容改变,当前内容为: %@",self.textField3.text);
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
-(void)dealloc{
    [self.textField3 removeObserver:self forKeyPath:@"text" context:nil];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

4.代理 
可以判断接下来是否允许继续输入

#pragma mark - 代理
-(void)addDelegate{
    //实现 UITextFieldDelegate 协议
    self.textField4.delegate = self;
}
#pragma mark  UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}// return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"textField4 - 开始编辑");
}// became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES;
}// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"textField4 - 结束编辑");
}// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0){

}// if implemented, called in place of textFieldDidEndEditing:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
     NSLog(@"textField4 - 正在编辑, 当前输入框内容为: %@",textField.text);
    return YES;
}// return NO to not change text

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值