Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone app - detect which tab bar item was pressed

i have a tab bar based application, with more than 5 tab bar items - so i get 4 of them directly showing in the view and the rest available by selecting the "More" tab. When a tab bar item is pressed, i want to detect which one was it.
So, in the
- (void)tabBarController:(UITabBarController *)tabBarCtrl didSelectViewController:(UIViewController *)viewController method, i use tabBarCtrl.selectedViewController.title to get the item's title.

This works for the tabs visible in the view -that is the 4 first and the "More" tab- but does not work for the rest of my tab bar items which are shown in the list after pressing the "More" tab.

I can see that the didSelectViewController method is not even called when selecting a tab from the "More" list.
How can i detect any of them when pressed?

Thank you in advance.

like image 231
CdB Avatar asked May 04 '11 15:05

CdB


People also ask

What is UITabBarController?

A container view controller that manages a multiselection interface, where the selection determines which child view controller to display.

Where is tab bar in iOS?

The Tab Bar is the instance of the UITabBar class, which inherits UIView. The tab bar always appears across the bottom edge of the screen.

How do I add a tab bar to my navigation controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.


2 Answers

How to get title of UITabBarItem in the More section?

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);

    if (viewController == tabBarController.moreNavigationController)
    {
        tabBarController.moreNavigationController.delegate = self;
    }
}
like image 88
0x8badf00d Avatar answered Oct 07 '22 20:10

0x8badf00d


You can access the index of selected item by using following code in your UIViewController. It will always return yout tab's index.

self.tabBarController.selectedIndex;

So if you have e.g. 6 items you can go to the "More..." tab, select your "5th" item and selectedIndex will be 4. If you go to the More tab and select 6th item, it'll return 5.


EDIT: If you want to check current position of some UITabBarItem you can do this:

Firstly, in your XIB file you should edit the tag property of each tab, so that 1st tab will have tag = 100, 2nd - 200, 3rd - 300, etc.

Then in ViewController add this code:

UIViewController *selectedVC = [self.tabBarController.viewControllers objectAtIndex:self.tabBarController.selectedIndex];
int selectedItemTag = selectedVC.tabItem.tag;

Then you can determine what viewController is it by using selectedItemTag variable. In this case, you can determine selectedIndex by doint this: selectedIndex = (selectedItemTag-100)/100.

The tag properties are not changed when customizing your UITabBar, so you can trust them :)

like image 45
akashivskyy Avatar answered Oct 07 '22 20:10

akashivskyy