0

I can't seem to add a badge on a TabBarItem Tried a lot of options (that's why the code is splitted into variables).
Thought it had something to do with the treading so the update is back on the main thread, still nothing.

The code:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    NSInteger badge_count = 0;
    badge_count = getDataFromServer();
    if (snacks_count > 0)
    {
        MainTabBarViewController *c = [self.storyboard instantiateViewControllerWithIdentifier:@"tabBarController"];
        UINavigationController *nav = [c.viewControllers objectAtIndex:1];
        dispatch_async(dispatch_get_main_queue(), ^{
            nav.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", (long)badge_count];
        });
    }
});
Boaz
  • 4,864
  • 12
  • 50
  • 90
  • are you sure that object of navigationcontrol its currect or not set 0 instead of 1 in objectAtIndex – Nitin Gohel Jun 16 '14 at 09:02
  • I want the badge to be on the second TabBarItem (index 1) – Boaz Jun 16 '14 at 09:02
  • UINavigationController *nav = [c.viewControllers objectAtIndex:1]; i am talking about that navigation controller object is i think not correct. – Nitin Gohel Jun 16 '14 at 09:04
  • view controllers is an array of all the TabBar viewControllers c.viewControllers <__NSArrayM 0x10c450be0>( , , , , , ) – Boaz Jun 16 '14 at 09:06
  • even when I make a breakpoint and dive into nav - I see the BadgeValue as 1 (and the title is the correct title so I'm sure it's the right object) - I just don't see anything on screen... – Boaz Jun 16 '14 at 09:16

2 Answers2

0

Please pay attention to the method: :instantiateViewControllerWithIdentifier" description: Instantiates and returns the view controller with the specified identifier. You use this method to create view controller objects that you want to manipulate and present programmatically in your application. Before you can use this method to retrieve a view controller, you must explicitly tag it with an appropriate identifier string in Interface Builder.

Namely, you create another instance. You should change to

 UINavigationController *nav = [self.viewControllers objectAtIndex:1];

in order to change the UINavigationController that you refer to

Gal Marom
  • 8,499
  • 1
  • 17
  • 19
0

Apparently the original problem was that I didn't do it from the main thread. Probably made a wrong code with all the playing around trying to debug.

The working code:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
badge_count = getDataFromServer();
if (snacks_count > 0)
{            
    NSInteger badge_count = 0;
    badge_count = getDataFromServer();
    if (badge_count > 0)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [(UIViewController *)[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem].badgeValue = [NSString stringWithFormat:@"%ld", (long)badge_count];
        });
    }
});
Boaz
  • 4,864
  • 12
  • 50
  • 90