Search code examples
iosobjective-cuitabbaruitabbaritem

How do we add action to Tab Bar item


I am using Tab Bar i.e. Bottom bar and have 5 tab bar items, i want to assign a method to each, so that i can navigate to other views on tab bar item click.

I've been looking for some clues, but couldn't make it.


Solution

  • Use UITabBarDelegate.

    Implement your class and inherit the protocol by adding after your class definition

    @interface MyViewController : UIViewController<UITabBarDelegate>
    

    and then use method tabBar:didSelectItem: in that class

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
        // Do Stuff!
        // if(item.title == @"First") {...}
    }
    

    May be it will help you