項目中有個驗證碼輸入直接驗證跳轉頁面,用的RAC來監聽textfield的輸入值,以下:session
@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; } }];
打印以下:spa
明顯走了兩次,還沒找到緣由,有幸看到的大神能夠幫忙解惑下!.net
替換方法以下:代理
一、直接監聽 code
#pragma mark - 直接添加監聽方法 -(void)addTargetMethod{ [self.textField1 addTarget:self action:@selector(textField1TextChange:) forControlEvents:UIControlEventEditingChanged]; } -(void)textField1TextChange:(UITextField *)textField{ NSLog(@"textField1 - 輸入框內容改變,當前內容爲: %@",textField.text); }
二、NSNotificationCenter 添加監聽方法server
#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]; }
三、代理方法--這種方法比較經常使用,可是代碼寫的比較多,若是TF多的話,看着可亂blog
#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
四、KVO監聽數值變化rem
-(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]; }
上面這幾種方法,親測有效,僅作記錄!get
參考:https://blog.csdn.net/qxuewei/article/details/50727617string