I am trying to implement “previous,” “next,” and “done” buttons for a series of UITextFields, each of which is contained in a UITableViewCell in a grouped UITableView. I hold on to the UITextFields in an NSMutableArray, and keep an integer pointing to the UITextField that is currently active. Here are the two selectors that get fired when the Previous and Next buttons are tapped, respectively.
-(IBAction)didSelectPreviousButton:(id)sender
{
if ((textFieldIndex - 1) >= 0) {
UITextField *currentField = [self.testTextFields objectAtIndex:(textFieldIndex)];
UITextField *nextTextField = [self.testTextFields objectAtIndex:(--textFieldIndex)];
BOOL result = [nextTextField becomeFirstResponder];
NSLog([NSString stringWithFormat:@"currentField's window: %@", currentField.window]);
NSLog([NSString stringWithFormat:@"nextTextField's window: %@", nextTextField.window]);
} else {
[self dismissKeyboard:sender];
}
}
-(IBAction)didSelectNextButton:(id)sender
{
if ((textFieldIndex + 1) < [self.inspectionItemSpec.numberOfTests intValue]) {
UITextField *currentField = [self.testTextFields objectAtIndex:(textFieldIndex)];
UITextField *nextTextField = [self.testTextFields objectAtIndex:(++textFieldIndex)];
BOOL result = [nextTextField becomeFirstResponder];
NSLog([NSString stringWithFormat:@"currentField's window: %@", currentField.window]);
NSLog([NSString stringWithFormat:@"nextTextField's window: %@", nextTextField.window]);
} else {
[self dismissKeyboard:sender];
}
}
As you can see, I am logging the window property of the current & next text field, and in the didSelectNextButton, everything is correct. However, in didSelectPreviousButton, nextTextField.window is always nil. Why would this be happening?
(Note that the previous button is enabled only after the user has tapped the next button once.)
This may be because each
UITextFieldis in aUITableViewCellwhile also being referenced inself.testTextFields. Because of the way cells are re-used by tables in iOS, you could (and probably will) end up in a situation where the next text field in your array is not the text field in the next visible row in the table.If you post your
tableView:cellForRowAtIndexPath:code, that may make the problem apparent.