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
Use the self.tabBarController.selectedViewController
property.
UPDATE: To get the index of a specific viewController, use:
NSUInteger index = [self.tabBarController.viewControllers indexOfObjectIdenticalTo:specificViewController];
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With