iOS开发之--为UITextField监听数值变化的三种方法

项目中有个验证码输入直接验证跳转页面,用的RAC来监听textfield的输入值,如下:

@weakify(self);
    [self.codeView.textField.rac_textSignal subscribeNext:^(NSString *value) {
        @strongify(self);
        
        self.value = value;
        //也可以直接在这里写想要执行的操作
    }];
    
    //当self.value的值变化时调用Block,这是用KVO的机制,RAC封装了KVO
    [RACObserve(self, self.value) subscribeNext:^(NSString *value) {
        
        NSLog(@"--%@",value);
        
        if (value.length == 6) {
            [self.navigationController pushViewController:[SetPsdViewController new] animated:YES];
            return;
        }
    }];

打印如下:

iOS开发之--为UITextField监听数值变化的三种方法

明显走了两次,还没找到原因,有幸看到的大神可以帮忙解惑下!

 

替换方法如下:

1、直接监听 

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

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];
}

3、代理方法--这种方法比较常用,但是代码写的比较多,如果TF多的话,看着可乱

#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

4、KVO监听数值变化

-(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];
}

上面这几种方法,亲测有效,仅做记录!

参考:https://blog.csdn.net/qxuewei/article/details/50727617

上一篇:iOS13崩溃 'NSGenericException', reason: 'Access to UITextField's _placeholderLabel


下一篇:在Ubuntu下搭建ASP.NET 5开发环境