I am working on an iOS application where a button is enabled only after all UITextField
s 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?
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.