Newbie obj-c question.
I have a custom tableview with four custom cells. In every cell is a editable textfield for customer info. I need to improve switching between textfields by Input Accessory View. http://uaimage.com/image/62f08045
I created an NSMutableArray with capacity 4. I tagged textfields and added them in this array in text field delegate method:
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
FDTextFieldCell *cell = (FDTextFieldCell *)[textField superview];
NSIndexPath *indexPath = [[self tableView] indexPathForCell:cell];
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
[[FDProfile sharedProfile] setName:[textField text]];
[textField setTag:1];
[textFieldsArray insertObject:textField atIndex:0];
} else if ([indexPath row] == 1) {
[[FDProfile sharedProfile] setSurname:[textField text]];
[textField setTag:2];
[textFieldsArray insertObject:textField atIndex:1];
} else if ([indexPath row] == 2) {
[[FDProfile sharedProfile] setNickname:[textField text]];
[textField setTag:3];
[textFieldsArray insertObject:textField atIndex:2];
} else if ([indexPath row] == 3) {
[[FDProfile sharedProfile] setEmail:[textField text]];
[textField setTag:4];
[textFieldsArray insertObject:textField atIndex:3];
[textField resignFirstResponder];
}
}
[[self tableView] reloadData];
return YES;
}
Now i try to improve functionality of button "Next":
- (void) inputAccessoryViewDidSelectNext:(FDInputAccessoryView *)view {
for (UITextField *textField in [self textFieldsArray]) {
if ([textField isFirstResponder]) {
textField = [[self textFieldsArray] objectAtIndex:textField.tag + 1];
[textField becomeFirstResponder];
}
}
}
But this code doesn't works. I think there is an issue with using fast enumeration?
Can somebody help with this? Thanks, Alex.
Some ideas to solving from c-had:
- Why are you setting up your textFieldsArray in textFieldShouldReturn:? Shouldn't this be done somewhere where initialization is done (e.g. viewDidLoad of the view controller)? The problem might be that your array is never actually set up, or is only partly set up. Also, putting the setup here means it will be called over and over, mutating your array each time and screwing things up.
- Your tags are off by one. You should be assigning them starting at 0, not 1 (or accounting for the difference in inputAccessoryViewDidSelectNext:). If your code was working, it would skip 2 text fields, as the first field would return tag 1, and you'd skip to objectAtIndex:2, which is the 3rd field.
- You don't account for going off the end. If you're at the last field, incrementing will not go back to the beginning.
I'd actually recommend not using tags at all. Instead, at setup time, just loop through the fields adding them in order to your array. Then, in inputAccessoryViewDidSelectNext:, use a counter instead of fast enumeration to figure out where you are.