iOS 提升交互之根据多个UITextField的编辑状态改变按钮状态

最近仿照支付宝登录根据账号密码TextField编辑状态改变登录按钮状态的效果封装了一个方法. 在UITextFieldDelegate的

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

方法中调用即可达到想要的效果:
iOS 提升交互之根据多个UITextField的编辑状态改变按钮状态_第1张图片

封装的代码:

- (BOOL)getButtonEnableByCurrentTF:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string tfArr:(NSArray *)tfArr;{
    if (string.length) {// 文本增加
        NSMutableArray *newTFs = [NSMutableArray arrayWithArray:tfArr];
        [newTFs removeObject:textField];
        for (UITextField *tempTF in newTFs) {
            if (tempTF.text.length==0) return NO;
        }
    }else{// 文本删除
        if (textField.text.length-range.length==0) {// 当前TF文本被删完
            return NO;
        }else{
            NSMutableArray *newTFs = [NSMutableArray arrayWithArray:tfArr];
            [newTFs removeObject:textField];
            for (UITextField *tempTF in newTFs) {
                if (tempTF.text.length==0) return NO;
            }
        }
    }
    return YES;
}

调用实例:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    // 提升交互:动态设置按钮可行不可行
    NSArray *tfs = @[_accountTF, _pswTF];

    if ([self getButtonEnableByCurrentTF:textField shouldChangeCharactersInRange:range replacementString:string tfArr:tfs]) {
        [_loginButton changeStyleToStandard];// 置为可用
    }else{
        [_loginButton ZS_standardDisable];// 置为不可用
    }
    return YES;
}

你可能感兴趣的:(iOS开发)