views:

236

answers:

2

Hi everyone,

I have a tabBarController set up in the AppDelegate and have a few UIViewControllers with Nav Controllers. In one of the TabBar items, after I have pushed a few UIViews I want to update the badgeValue item of a different TabBar item.

Whats the best way to do this? The only way I can really think is a NSNotification and a singleton storage for the value, but it seems a lot of work for something simple, that and I have no idea about NSNotifications.

I had a wild guess at something like super.tabBarController.otherView.tabBarItem.badgeValue = @"1" (as I set which tab is selected in a similar way) but I'm not surprised this doesn't work.

Thanks

+1  A: 

I don't have the code on hand right now, but it should be something more like

...otherViewController.tabBarItem.badgeValue = 1;
alku83
True, I've changed my original question, didn't help fix it though :(
Rudiger
Make sure you're setting the badge to an integer value, not an NSString value. If that's still not working you could creating an IBOutlet for the TabBarItem and reference it that way.
alku83
I have actually set it in the AppDelegate to make sure it works and it accepts a string. I'm not so sure about the IBOutlet as I add everything to the nib via code, IBOutlet would only work if its been setup in the nib
Rudiger
What about iterating over the TabBarController's viewControllers? I would go straight to the source (eg. AppDelegate), as opposed to super.tabBarController. Still, all of this seems a bit like a workaround, have you checked to see if the tabBarItem that's being referenced is not actually nil?
alku83
In the same area of code I have [super.tabBarController setSelectedIndex:2] and that works fine. I think its possible through something like this as badgeValue is a property of UITabBarItem, and UITabBarItem is a property of UITabBarController and I know I can access UITabBarController via super.TabBarController, but can't figure out how to update a certain UITabBarItem.
Rudiger
How about [(UIViewController*)[myTabBar.viewControllers objectAtIndex:1] tabBarItem].badgeValue = @"1";
alku83
I know that wouldn't work but you pointed me in the direction to get it. Participation point for you :)
Rudiger
Glad we got there eventually!
alku83
+4  A: 

Thanks to alku83 who pointed me in the right direction the code is:

[[super.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue = @"1";
Rudiger