I have 3 UITextFields in a grouped UITableView and am trying to figure out the correct logic to only have my 'Save' UIBarButtonItem enabled when none of the UITextFields are empty.
I'm currently using the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
UITextField delegate method to detect changes to the field character by character, but it is providing inconsistent results.
Any ideas?
Edit: Here is the code I'm now using. As you can see I've placed my text fields into an array so I can iterate through them. As it is now, the save button doesn't enable until I enter the 2nd character in the 3rd field. Also it alternates enabled/disabled as a remove characters one by one from the fields.
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;