做登录时,判断密码输入长度或是否满足其他要求来控制登录按钮的一些变化。非常简单,记录一下需要用的时候看一看。
@implementation ViewController
{
UITextField *textField;
UIButton *okButton;
}
- (void)viewDidLoad {
[super viewDidLoad];
textField = [[UITextField alloc]initWithFrame:CGRectMake(40, 200, self.view.frame.size.width-80, 60)];
textField.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:textField];
okButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-180, 300, 80, 40)];
[okButton setTitle:@"确认" forState:UIControlStateNormal];
okButton.backgroundColor = [UIColor lightGrayColor];
okButton.userInteractionEnabled = NO;
[self.view addSubview:okButton];
//两种方式实时监控输入框的变化
//1
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showtextFiledContents) name:UITextFieldTextDidChangeNotification object:nil];
//2
[textField addTarget:self action:@selector(showtextFiledContents) forControlEvents:UIControlEventEditingChanged];
}
- (void) showtextFiledContents{
//NSLog(@"%@",textField.text);
if (textField.text.length > 3) {
okButton.backgroundColor = [UIColor greenColor];
okButton.userInteractionEnabled = YES;
}else{
okButton.backgroundColor = [UIColor lightGrayColor];
okButton.userInteractionEnabled = NO;
}
}
@end