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;
Ok here’s how I finally did it.
I created
- (IBAction)validateFields:(id)senderand connected it to the Editing Changed outlet on UITextField. It looks like this.I’ve given it a pretty decent go and couldn’t get the save button to enable on any combination of empty text fields so I’m going to say this is the way to go.