views:

257

answers:

2

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;
A: 

How exactly are you using the method? Here's how I would do it:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  if (newString.length) {
    //If all the others are also non-empty, enable your button
  }
  return YES;
}

But it might make more sense for you to enable your button after the user presses the enter key, which is easier to deal with. Just use a didEndEditingOnExit or didEndEditing event on the text fields and check there to see if they're non-empty.

Ed Marty
I updated my question to clarify what I am trying to do.
CrystalSkull
A: 

Ok here's how I finally did it.

I created - (IBAction)validateFields:(id)sender and connected it to the Editing Changed outlet on UITextField. It looks like this.

- (IBAction)validateFields:(id)sender
{
    BOOL valid = YES;

    // On every press we're going to run through all the fields and get their length values. If any of them equal nil we will set our bool to NO.
    for (int i = 0; i < [textFieldArray count]; i++)
    {
        if (![[[textFieldArray objectAtIndex:i] text] length])
            valid = NO;
    }

    [saveButton setEnabled:valid];
}

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.

CrystalSkull