Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS 5: Strange behavior when jumping in between TextFields
    primarykey
    data
    text
    <p>In my app I have a pretty large form to be filled out. The form itself is made with a table view and static cells mode. Each of the cells containing a label and a textfield. I wanted to add a toolbar above the keyboard to enable navigating between the text fields. I did that, however it is behaving very strange (at least I think).</p> <p>I have no problems jumping to the next text field while jumping to the previous textfield is only possible if it is visible. If it is not visible the old textfield stays first responder, however the moment I scroll my table view and the intended text field becomes visible, it becomes the first responder (I do not click into it!). Well this is of course not the behavior that I wanted.</p> <p>My question is: Is this kind of behavior normal? Could you somehow circumvent it?</p> <p>If you want to see this kind of behavior yourself I have uploaded an Xcode project that illustrates the problem. You can download the zipped project here: <a href="http://www.patrickstummer.com/ios/TestJumpTextfields.zip" rel="nofollow">download</a>. The main parts of the code are explained below. </p> <p>I set up a grouped table view with static cell. Each of them containing a label and a <code>textfield</code>. I created outlets for every textfield to gain access to them. In my viewDidLoad method I create the toolbar and its buttons, store the textfields in an array and set the controller to be the textfields delegate.</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; // create the keyboard toolbar with navigation elements self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStyleBordered target:self action:@selector(prevClicked:)]; UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(nextClicked:)]; [self.keyboardToolbar setItems:[NSArray arrayWithObjects:prevButton, nextButton, nil]]; // create the field chain self.fields = [NSArray arrayWithObjects:self.aField, self.bField, self.cField, self.dField, self.eField, self.fField, nil]; // for scrolling and keeping track of currently active field NSInteger max = [self.fields count]; for(NSInteger i = 0; i &lt; max; i++) { UITextField *curr = (UITextField *)[self.fields objectAtIndex:i]; curr.delegate = self; } } </code></pre> <p>The text field delegate methods store a reference of the active text field and prevent line breaks from being inserted.</p> <pre><code>- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { textField.inputAccessoryView = self.keyboardToolbar; return YES; } - (void)textFieldDidBeginEditing:(UITextField *)textField { // set the current active textfield self.currentField = textField; // scroll this textfield to top UITableViewCell *cell = (UITableViewCell *)textField.superview.superview; [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; self.currentField = nil; return NO; } </code></pre> <p>And finally there are the selectors for the toolbar buttons and the logic to get the next textfield.</p> <pre><code>- (void)moveResponderByStep:(NSInteger)step { // only move if a textfield is the current responder if(![self.currentField isEditing]) { return; } // where we are and where to move NSInteger max = [self.fields count]; NSInteger moveToNumber; for(NSInteger i = 0; i &lt; max; i++) { if(self.currentField == [self.fields objectAtIndex:i]) { moveToNumber = i + step; } } // stay in bounds if(moveToNumber &gt;= max || moveToNumber &lt; 0) { [self.currentField resignFirstResponder]; return; } // move on UITextField *next = [self.fields objectAtIndex:moveToNumber]; [next becomeFirstResponder]; } - (void)prevClicked:(id)sender { [self moveResponderByStep:-1]; } - (void)nextClicked:(id)sender { [self moveResponderByStep:1]; } </code></pre> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload