5

I have a bunch of text fields in a registration form that are organized in two vertical stack views in a XIB file. I noticed that when running the app in the simulator I can press the Tab key and iOS will automatically move on to the next text field.

But this doesn't work sometimes, and I was wondering why. Sometimes the system focuses a text field from another stack view instead of the field below it.

I have already set up a chain of Next text fields (when pressing Next on the software keyboard) like this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    NSUInteger index = [self.textFields indexOfObject:textField];
    if (index != NSNotFound) {
        if (index == self.textFields.count - 1) {
            [textField resignFirstResponder];
            [self createAccount];
        } else {
            UITextField *nextTextField = self.textFields[index + 1];
            [nextTextField becomeFirstResponder];
        }
        return NO;
    }
    return YES;
}

textFields is an array of all text fields that this screen has from top to bottom.

I just want to be able to fill the form fast by typing a few characters and pressing Tab without additional mouse clicks. Is it possible?

It seems that -textFieldShouldReturn is not called during this "tab switch" so I can't control which text field becomes the next first responder. What is the trick for getting them to focus in the right order?

iosdude
  • 1,131
  • 10
  • 27
  • Possible duplicate of [How to navigate through textfields (Next / Done Buttons)](https://stackoverflow.com/questions/1347779/how-to-navigate-through-textfields-next-done-buttons) – atulkhatri Aug 09 '17 at 05:54
  • Please double check your `self.textFields`. Maybe it has wrong order. Or try to remove ref and then connect again with the order you want. – tuledev Aug 09 '17 at 06:08
  • Another interesting contribution to this topic: https://stackoverflow.com/a/6177614/6319106 – clemens Aug 09 '17 at 06:12
  • @anhtu I checked one more time, the order is correct. – iosdude Aug 09 '17 at 06:39
  • @atulkhatri It's not a duplicate, I have already implemented navigation to next text field via Next buttons as described in that question (which I think is different from Tab navigation). – iosdude Aug 09 '17 at 06:41

1 Answers1

-1

Check the index that returns by indexOfObject:. If you have some equal objects in the array, this method returns the index of the first of them.

If the index is wrong, use the tag property of UITextField like this:

UITextField * nextResponder = [self.textFields objectAtIndex:textField.tag + 1];

or if all of your text fields are in the same superview:

UITextField * nextResponder = (UITextField*)[textField.superview viewWithTag:textField.tag + 1];

Apple documentation:

Starting at index 0, each element of the array is passed as an argument to an isEqual: message sent to anObject until a match is found or the end of the array is reached. Objects are considered equal if isEqual: (declared in the NSObject protocol) returns YES. https://developer.apple.com/documentation/foundation/nsarray/1417076-indexofobject?language=objc

This method defines what it means for instances to be equal. For example, a container object might define two containers as equal if their corresponding objects all respond YES to an isEqual: request. See the NSData, NSDictionary, NSArray, and NSString class specifications for examples of the use of this method.

If two objects are equal, they must have the same hash value. This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass. https://developer.apple.com/documentation/objectivec/1418956-nsobject/1418795-isequal?language=objc

Earl0Grey
  • 497
  • 7
  • 10