我在一个分组的 UITableView 中有 3 个 UITextFields,我试图找出正确的逻辑,以便在没有任何 UITextFields 为空时启用我的“保存”UIBarButtonItem。
我目前正在使用- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
UITextField 委托方法逐字符检测字段的更改,但它提供的结果不一致。
有任何想法吗?
编辑:这是我现在使用的代码。如您所见,我已将文本字段放入数组中,以便遍历它们。就像现在一样,在我在第三个字段中输入第二个字符之前,保存按钮不会启用。它还交替启用/禁用作为字段中的一个一个删除字符。
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
BOOL allValid;
if (newString.length)
{
// Cycle through array checking for completeness
for (int i = 0; i < [textFieldArray count]; i++)
{
if ([[[textFieldArray objectAtIndex:i] text] length] > 0)
{
allValid = YES;
NSLog(@"TextField #%i Validates.", i);
}
else
{
allValid = NO;
NSLog(@"TextField #%i Does Not Validate.", i);
}
}
}
else
{
NSLog(@"Invalid");
allValid = NO;
}
if (allValid)
[saveButton setEnabled:YES];
else
[saveButton setEnabled:NO];
return YES;