本篇文章只為幫助跟多的人。適合初學者。
在這里我介紹3種監聽UITextField的方法。並在最后寫了一個小的demo 提供參考。
-------請不要糾結小編的命名方式規不規范,一切只為共同學習,共同進步。
@property (weak, nonatomic) IBOutlet UITextField *UserID;
@property (weak, nonatomic) IBOutlet UITextField *Password;
@property (weak, nonatomic) IBOutlet UIButton *LoginBut;
監聽文本框中的內容的幾種方式:(用於登錄賬號-用於搜索)
1.代理 (只能監聽設置了代理的文本框沒有設置代理的文本框不能進行監聽)
<UITextFieldDelegate>
_UserID.delegate=self(設置代理)
//是否可以編輯yes可以 no不可以
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
//開始編輯的時候調用
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"開始編輯");
}
//是否允許結束編輯(當用戶結束一個文本框的時候,首先會調用這個)
//如果是NO 意思就是,這個文本框回一直處於編輯狀態(也叫第一響應者),就算你再點擊其他文本框,是沒有任何效果的
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
//結束編輯的時候調用
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"結束編輯");
}
//是否允許用戶輸入文件
//用戶每次輸入一個字符的時候就會調用一次。然后判斷是非顯示在文本框里
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//打印
NSString *text=[NSString stringWithFormat:@"%@%@",textField.text,string];
NSLog(@"------%@",text);
return YES;
}
//是否允許清除當前文本框中所輸入的內容
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
2.通知 (可以監聽所有的文本框)
/*
UITextFieldTextDidChangeNotification //文本框改變的通知
object:_UserID 表示_UserID 這一個文本框
object:nil 表示 所有的文本框
*/
[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:nil];
-(void)textChange{
NSLog(@“-----Change-----");
}
//當前對像要銷毀的時候,創建的通知要進行移除:(否則可能報錯,壞內存訪問)
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
3.監聽者(addTarget)()
/*
Events(事件):當我產生什么事件的時候會掉用這個方法
UIControlEventEditingChanged : 當文本框進行編輯
*/
[_UserID addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
[_Password addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
}
//我把2個文本框的監聽觸發方法都設置為textChange 目的是判斷登錄按鈕是否可以點擊
//只有2個文本框都有值才可以進行點擊(這里根據自己的需要)
-(void)textChange{
if(_UserID.text.length && _Password.text.length){
_LoginBut.enabled=YES;
}else{
_LoginBut.enabled=NO;
}
//這個判斷可以優化為一行:
_LoginBut.enabled=_UserID.text.length && _Password.text.length;
NSLog(@"gaibia-----");
}
demo
通知與代理一起使用:
如果有5個文本框或者跟多同時監聽,我想直接打印出處於編輯的那個文本框(第一響應者)是哪一個,內容是什么:
代碼:


這里不需要拖線(你的storyboard 要與你的viewcontroller相關聯)
#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate>
//@property (nonatomic, strong) NSArray *TextFieldAll;//存儲所有的textField
@property (nonatomic, strong) UITextField *newtext;//獲取處於編輯狀態的UITextField
@end
@implementation ViewController
- (void)viewDidLoad {
//創建一個數組存儲textfield
NSMutableArray *fieldsM = [NSMutableArray array];
//沒有拖線
//這句代碼是獲取view上的所有控件,包括label,textfield;
NSArray *childArray=self.view.subviews;
//循環遍歷所有的控件
for (UIView *child in childArray) {
//找出所有的TextFieldAll
if([child isKindOfClass:[UITextField class]]){
//類型轉換
UITextField *textField=(UITextField *)child;
//設置代理
textField.delegate=self;
// [fieldsM addObject:textField];
}
}
// _TextFieldAll=fieldsM;
//創建通知:監聽每一個TextField
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:nil];
}
-(void)textChange{
//打印處於編輯狀態的TextField 和 tag
NSLog(@"%@-%ld",_newtext.text,(long)_newtext.tag);
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
_newtext=[[UITextField alloc]init];
_newtext=textField;
}
@end
最后運行效果 和 打印——————————————————
