【发布时间】:2010-04-14 18:31:35
【问题描述】:
我在一个分组的 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;
【问题讨论】:
标签: iphone cocoa-touch uitextfield uibarbuttonitem