Take the 2-minute tour ×

Any idea why this works:

[[[[[self tabBarController] tabBar] items] objectAtIndex:2] 

But this doesn't?

[[self tabBarItem] setBadgeValue:@"Hello"];

I would rather not have to provide an explicit tab bar index (2 in the code above). Also, is there a way to get the tab bar index from within the view controller in the case where the first snippet is the only way to make this work?


share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Is the tabBarItem you refer to there actually an item in the tabBarViewController? If its not then theres your problem. You can know the index of your viewController because u specifiy the order when you set the view controllers in your tabBarviewCOntroller, so if you set viewController a first in your tabBarViewController then its button will have an index of 0.

With your comment your question become more clear to me, as the reply stated, perhaps once you set the UITabBarController viewController, it grabs its tabBarItem and later changes to that viewController property dont get updated by the tabBarViewController (i might be wrong this is a guess)

share|improve this answer
Can you clarify a bit? I was under the impression that if you add a view controller to a tab bar in IB, then the tabbaritem in your view controller would be automatically set. The variable tabBarItem is definitely not nil and it has the correct title, but it isn't the same address as the one accessed through the tabBar items array. –  Matt Long Sep 8 '09 at 20:45
Just found this in the Apple docs: "Tab bar items are configured through their corresponding view controller. To associate a tab bar item with a view controller, create a new instance of the UITabBarItem class, configure it appropriately for the view controller, and assign it to the view controller’s tabBarItem property. If you do not provide a custom tab bar item for your view controller, the view controller creates a default item containing no image and the text from the view controller’s title property." Think I'm just going to access it through the array by index rather than creating one. –  Matt Long Sep 8 '09 at 20:48
Ah i see what you were doing now, it seems as though the example you gave should work...what im thinking might be happening is that once you assign a viewController to your tababrcontroller it captures that tabbaritem and later changes to the viewController tabBarItem property dont update t he button because the tabBarViewController doesnt check for the updates.. –  Daniel Sep 8 '09 at 21:15
add comment

Your Answer


