需要用到的输入框全局化,并分别添加代理方法:
@property (retain, nonatomic) UITextField *idTextField;
@property (retain, nonatomic) UITextField *passwordTextField;
[_idTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[_passwordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
描述代理方法并做判断:
#pragma mark -输入框实时变化改变颜色
-(void)textFieldDidChange :(UITextField *)theTextField{
if ([_idTextField.text isEqualToString:@""]) {
if ([_passwordTextField.text isEqualToString:@""]) {
_loginButton.backgroundColor = [UIColor lightGrayColor];}
else{
_loginButton.backgroundColor = [UIColor lightGrayColor];
}
}else{
if ([_passwordTextField.text isEqualToString:@""]) {
_loginButton.backgroundColor = [UIColor lightGrayColor];}
else
{
_loginButton.backgroundColor = [UIColor blueColor];}
}
}