在使用UITextField來判斷登陸按鈕狀態時只有
shouldChangeCharactersInRange函數,是在文件還沒有改變前就調用了,而不是在改變后調用,要想實現改變后調用的功能,導致登陸按鈕顯示狀態不准確,我們可以增加事件監聽的方式
先來看看objective-c提供的接口:
// add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event.
// passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order
// the action cannot be NULL.
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
使用方法:
//第一步,對組件增加監聽器
[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
...
//第二步,實現回調函數
- (void) textFieldDidChange:(id) sender {
UITextField *_field = (UITextField *)sender;
NSLog(@"%@",[_field text]);
}
