UITabBar(Controller) - Get index of tapped?
Solution 1
The answer depends on whether or not the UITabBar is managed by a UITabBarController or not.
Case 1 - UITabBar is already handled by a UITabBarController
Implement the UITabBarControllerDelegate
protocol. Specifically the tabBarContoller:didSelectViewController:
method. Set an instance of your class that implements the protocol as the delegate
of the UITabBarController
.
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
NSLog(@"Tab index = %u (%u)", (int)indexOfTab);
}
In this case you have to be aware of the special situation where you have enough controllers in the tab controller to cause the "More" tab to be displayed. In that case you'll receive a call to the tabBarController:didSelectViewController:
with a view controller that isn't in the list (it's an instance of an internal UIKit class UIMoreNavigationController). In that case the indexOfTab
in my sample will be NSNotFound
.
Case 2 - UITabBar is NOT already handled by a UITabBarController
Implement the UITabBarDelegate
protocol. Specifically the tabBar:didSelectItem:
method. Set an instance of your class that implements the protocol as the delegate
of the UITabBar
.
- (void)tabBar:(UITabBar *)theTabBar didSelectItem:(UITabBarItem *)item {
NSUInteger indexOfTab = [[theTabBar items] indexOfObject:item];
NSLog(@"Tab index = %u", (int)indexOfTab);
}
EDIT: Modified the method parameter variables to eliminate the OP's compilation warning about tabBarController
being hidden.
Solution 2
SWIFT:
// somewhere inside your TabBarViewController
//...
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let indexOfTab = tabBar.items?.index(of: item)
print("pressed tabBar: \(String(describing: indexOfTab))")
}
Solution 3
SWIFT 4:
I prefer
// somewhere inside your TabBarViewController
//...
func tabBarController(_ tabBarController: UITabBarController,
shouldSelect viewController: UIViewController) -> Bool{
let index = tabBarController.viewControllers?.index(of: viewController)
return true// you decide
}
Solution 4
There's a method defined in the UITabBarDelegate
protocol called tabBar:didSelectItem:
, which will notify you of which and when a UITabBarItem
is selected (tapped).
Solution 5
I did it like this : This is in a custom class which extends UITabBarController
.h
@interface CustomTabBarController : UITabBarController<UITabBarDelegate>
.m
-(void)tabBar:(UITabBar *)theTabBar didSelectItem:(UIViewController *)viewController
{
NSLog(@"Tab index = %@ ", theTabBar.selectedItem);
for(int i = 0; i < theTabBar.items.count; i++)
{
if(theTabBar.selectedItem == theTabBar.items[i])
{
NSLog(@"%d",i);// this will give the selected tab
}
}
//NSlog(@"Items = %@", theTabBar.items[0]);
}
esqew
In my current role at EY, I assist some of the world's most prominent organizations in realizing the incredible power of enterprise-grade intelligent automation by building scalable, effective, and efficient strategies, operating models, and governance models for robotic process automation, machine learning, natural language processing, and optical character recognition technologies. I've also assisted on numerous initiatives within these same organizations to build, test, and deploy business process-specific applications of these technologies. Note to visitors from closed questions I believe strongly in the mission of Stack Overflow as a useful knowledge repository. As such, I volunteer my time and energy to maintaining the site's content quality in accordance with our guidelines. If I've left a comment on your post or voted to close your question, it's not a personal affront - it's always to ensure that this site remains as a helpful source of information for everyone. Content disclaimer My contributions to Stack Exchange are solely my own; any opinions that may appear in my contributions do not necessarily reflect the views of any of my current or former employers.
Updated on May 03, 2020Comments
-
esqew about 4 years
I've got a tab bar application and I need to know when and what button a user taps on the tab bar as to display the appropriate notifications and such.
In short: How would I go about detecting the index of a tapped UITabBarItem on a UITabBar?
Thanks in advance!
-
Bartłomiej Semańczyk almost 9 yearsPlease, consider this question as an answer:-) stackoverflow.com/questions/31521951/…
-
-
JaseC almost 9 yearsSorry but how do you figure out which case you are?