How does iOS decide which textbox to focus next when you press the Tab key on the simulator?

I have a bunch of text boxes in a registration form that are organized in two vertical stack views in an XIB file. I noticed that when I launch the app in the simulator, I can press the Tab key and iOS will automatically jump to the next text field.

But sometimes it doesn't work and I was wondering why. Sometimes the system will focus the text box from another stack view instead of the box below it.

I've already created a chain of the following text fields (when pressing Next on the software keyboard) as follows:

- (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 boxes this screen has from top to bottom.

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

It -textFieldShouldReturn

doesn't seem to be called during this "tab switch" so I have no control over which textbox becomes the next first responder. What's the trick for focusing them in the correct order?

+3


source to share


1 answer


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

If the index is not correct, use the UITextField tag property as follows:

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

      

or if all your text boxes are in the same supervision:

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

      



Apple Documentation:

Starting at index 0, each element in the array is passed as an argument to the isEqual: message sent to the object 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 determines what it means that the instances are equal. For example, a container object can define two containers to be equal if their respective objects all answer YES to the isEqual: request. See the NSData, NSDictionary, NSArray, and NSString specifications for examples of using this method.

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

0


source







All Articles