Question

I am working on an iOS application where a button is enabled only after all UITextFields that are filled in (i.e. have at least one character inside each one). Each UITextField is in a custom UITableViewCell inside of a UITableView. Once the user has entered a value in each UITextField, I need the button to be enabled. Inside my viewDidLoad method, I disable the button as follows:

[_doneButton setEnabled:NO];

and then I have the following method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    for (int i = 0; i < [self.table numberOfSections]; i++) {

        NSInteger rows =  [self.table numberOfRowsInSection:i];

        for (int row = 0; row < rows; row++) {

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:i];
            SimpleTableCell *cell = (SimpleTableCell *)[self.table cellForRowAtIndexPath:indexPath];

            for (UIView *subView in cell.subviews) {

                if ([subView isKindOfClass:[UITextField class]]) {
                    //Does not come to this point
                    UITextField *txtField = (UITextField *)subView;

                    if([[txtField text] length] > 0) {

                        //Enable button and bold title
                        [_doneButton setEnabled:YES];
                        [_doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:28]];

                    }

                     else {

                         [_doneButton setEnabled:NO];

                     }

                }

            }

        }

    }

    return YES;
}

Unfortunately, my code does not get into the "if" clause where I check to see if the custom cell has a subView of type UITextField, and because of that, the button is never enabled. For the record, "SimpleTableCell" is the name of my custom UITableViewCell class that I've created. My code appears to be correct, but I can't figure out why it is not functioning correctly. Can anyone see what it is I'm doing wrong?

Was it helpful?

Solution

I think I would go about this in a completely different way. I would create a mutable dictionary to hold the strings in the various text fields. Give the text fields tags, and use the tags (converted to NSNumbers) as the keys in the dictionary. Implement textFieldDidEndEditing:, and in that method get the string, check that it's not an empty string, and if not, do setObject:forKey: on the dictionary. After you set the value, check the count of the dictionary's allKeys property, and if it equals the number of text fields you have, enable the button.

OTHER TIPS

That's because the only logical subview of the cell is an UIView containing all the items of the cell. It's property is "contentView".

The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

More information about it here: https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/contentView

Have you set the UITextField's delegate? If that method is not getting called at all, that is the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top