Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to determine which tabbar index is my view controller

Can somebody please tell me how I can determine which tabbar index a view controller is at.

to simplify - I can jump to a tabBarItem at the moment by hardcoding its index.

self.tabBarController.selectedIndex = 3;

However, should the user customize the tab bar items, there is a possibility the viewController at number 3 isn't the one that the user will want as it has been moved. How can I determine where it has moved to so I can select the correct one.

Any help please.

Thanks,

Lee

like image 749
theiOSDude Avatar asked Mar 09 '11 09:03

theiOSDude


2 Answers

Use the self.tabBarController.selectedViewController property.

UPDATE: To get the index of a specific viewController, use:

NSUInteger index = [self.tabBarController.viewControllers indexOfObjectIdenticalTo:specificViewController];
like image 144
Ortwin Gentz Avatar answered Oct 02 '22 15:10

Ortwin Gentz


You can get the list of controllers in the UITabBar and compare by pointer value. For example, a view controller that is in a UITabBar can figure out it's location like this:

int loc = 0;
for (UIViewController *vc  in [self.tabBarController viewControllers]){
    if (vc == self.navigationController || vc == self){
        break;
    }
    loc++;
}
if (loc == [[self.tabBarController viewControllers] count])
     NSLog(@"Could not find me!");
else
     NSLog(@"Im in tab:% d",loc);
like image 36
fsaint Avatar answered Oct 02 '22 16:10

fsaint