Best way to update badgeValue of UITabBarController from a UIView
Asked Answered
C

3

13

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

Cushy answered 18/5, 2010 at 3:18 Comment(0)
C
38

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

[[super.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue = @"1";
Cushy answered 18/5, 2010 at 7:55 Comment(0)
R
7

I'm using Xcode 4.5 with Storyboards and iOS 6, so the answer may have changed since it was originally posted.

First you have to access the Tab Bar Controller like this:

UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;

Then you can set the badge like so:

[[tabController.viewControllers objectAtIndex:1] tabBarItem].badgeValue = @"New!";
Remount answered 29/9, 2012 at 15:31 Comment(1)
This is basically the same thing, difference is yours is accessing the UITabBarController though the window singleton, the accepted answer is accessing it through the parent of the current controller. Yours will work if the UITabBarController is the root view controller on the window, if not it will probably crash. The accepted answer will work from the direct child, but will get messy if you're trying to update from a controller a few layers deep.Cushy
M
0

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

...otherViewController.tabBarItem.badgeValue = 1;
Matchmark answered 18/5, 2010 at 3:36 Comment(8)
True, I've changed my original question, didn't help fix it though :(Cushy
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.Matchmark
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 nibCushy
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?Matchmark
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.Cushy
How about [(UIViewController*)[myTabBar.viewControllers objectAtIndex:1] tabBarItem].badgeValue = @"1";Matchmark
I know that wouldn't work but you pointed me in the direction to get it. Participation point for you :)Cushy
Glad we got there eventually!Matchmark

© 2022 - 2024 — McMap. All rights reserved.