6

I have a UIView which has a UITabBar with 4 UITabBarItem components in it (all created from IB).

I want my IBAction function called when someone clicks on the items in the tab bar. But I am unable to connect the tabbaritem to my action via IB... I control drag from the "received actions" but it does not allow me to connect that to the tabbaritem.

Thanks Deshawn

DeShawnT
  • 479
  • 2
  • 7
  • 12

3 Answers3

14

Control drag your tabbar to your "File's owner" in IB and set it as the delegate. Next drop this code in your viewcontroller.m file:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if(item.tag == 0)
    {
        //party like its 1999 right here
    }
}

Go back to IB and set each tab bar item in your tab bar with a tag. You gotta test for each case in your didSelectItem, so if you got more than one set a different tag etc. Thats how I did it anyway.

jayvatar
  • 199
  • 2
  • 9
  • 1
    This sounded like the most promising lead for me, but where the heck is this "File's owner" are you talk about? I don't see it anywhere. – SikoSoft May 04 '15 at 10:58
6

It is not clear from your question if you also defined a UITabBarController.

If you did not (as I assume, otherwise clicking on a tab bar item should work if you correctly defined things in IB), the way to go is assigning a UITabBarDelegate to your UITabBar and define tabBar:didSelectItem:

Have a look at the reference for UITabBarDelegate

sergio
  • 68,819
  • 11
  • 102
  • 123
0

You can't set a target action to your tab bar items. They should always have a new view. The only way is - you should use delegates which determine that your tabbar item is touched and then handle which tab is pressed in that delegate.

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if(item.tag == 1)
    {
    }
}

You should set a tag value to your tab bar item from the properties in xcode.

Anish
  • 319
  • 4
  • 9
  • 1
    Hi! What if one wants a "Touch down" action recognized on a TabBar Item (Instead of "selected" action)? How would that be done? – BadmintonCat Dec 07 '13 at 08:02