iOS 提升互動之根據多個UITextField的編輯狀態改變按鈕狀態
阿新 • • 發佈:2019-02-10
最近仿照支付寶登入根據賬號密碼TextField編輯狀態改變登入按鈕狀態的效果封裝了一個方法. 在UITextFieldDelegate的
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
方法中呼叫即可達到想要的效果:
封裝的程式碼:
- (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;
}