Search code examples
iphoneiosxcodetabbartabindex

How can I get indexOfObject from a Tab Bar?


I have an iPhone app terminating with the following error for the action.tag statement below. I'm in a delegate for a tab bar controller, trying to determine if the active class should continue on to a tab selected by the user or stay in the current class. If the decision is to go to another class I'm trying to get the appropriate tab bar index to pass back to the main tab bar controller.

'-[UITabBarController viewControllersindexOfObject:]: unrecognized selector

action.tag = [self.tabBarController viewControllersindexOfObject:viewController];

Much thanks to NJones for the earlier assistance with this code as part of an action sheet implementation he shared with me. The original example had a period between tabBarController and viewControllersindexOfObject but that was giving me a syntax error. I do get a warning on the current syntax that UITableController may not respond to viewControllersindexOfObject.


Solution

  • Well, I've never seen that method before and I don't believe it exists. But you can get the index of a view controller in a tab bar easily like that:

        [[self.tabBarController viewControllers] indexOfObject:viewController];
    

    Hope that helps you out.

    Joerg.