How do you set the tab order in iOS?

前端 未结 8 854
星月不相逢
星月不相逢 2020-12-08 18:45

Is there a way (either in IB or code) to set the tab order between text fields in a view?

Note that I\'m not talking about the next form field after the return (or

8条回答
  •  -上瘾入骨i
    2020-12-08 19:20

    I solved this by subclassing UITextField as NextableTextField. That subclass has a property of class UITextField with IBOutlet a hookup.

    Build the interface in IB. Set the class of your text field to NextableTextField. Use the connections Inspector to drag a connection to the 'next' field you want to tab to.

    In your text field delegate class, add this delegate method...

    - (BOOL)textFieldShouldReturn:(UITextField *) textField
    {
        BOOL didResign = [textField resignFirstResponder];
        if (!didResign) return NO;
    
        if ([textField isKindOfClass:[NextableTextField class]])
            dispatch_async(dispatch_get_current_queue(), ^{ [[(NextableTextField *)textField nextField] becomeFirstResponder]; });
    
        return YES;
    }
    

    BTW - I didn't come up with this; just remember seeing someone else's idea.

提交回复
热议问题