1

In my app, the badge value of my tabBar item is set in the AppDelegate.m, as following:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UITabBarController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"]; 
    [[tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"1"];
    ...
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    ...
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UITabBarController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"]; 
    [[tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"2"];
    ...
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    ...
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UITabBarController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"]; 
    [[tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"3"];
    ...
}

Problem is: The badge value is always "1". Why the badge value set in both applicationWillEnterForeground: and applicationDidBecomeActive: will never appear? If i did not set badge value in application: didFinishLaunchingWithOptions:, there is no badge shown there.

Rob
  • 4,927
  • 12
  • 49
  • 54
lu yuan
  • 7,207
  • 9
  • 44
  • 78
  • I have now the same problem. I set badge value into applicationDidBecomeActive. I can see the correct value into debug but when my tab bar is visualized, I lost badge value!!!! Why? There is a solution? – Blasco73 Dec 21 '13 at 09:13
  • @Blasco73 Plz check other's answers and replies:) – lu yuan Dec 21 '13 at 09:27

2 Answers2

3

You need to read up on the UIApplicationDelegate protocol, and the various state transitions. The applicationDidBecomeActive transition does not happen at launch, only when a set of state changes in the phone occur.

David H
  • 40,852
  • 12
  • 92
  • 138
  • Here the applicationDidBecomeActive called in my situation. problem is the badge set there will never appear on the tab bar item. Thx for you answer:) – lu yuan Jul 24 '12 at 12:18
  • The badge value has been modified as i can log it with the modified badge value. but it do not appear on the tab bar item. – lu yuan Jul 24 '12 at 12:22
  • I don't use storyboards, but when you modify the badge value the UITablBarController has to have been made the rootViewController of the window, and made visible, etc. You may be trying to create something and set its value only to have the object overwritten later. If you can defer updating the badge item til you are sure the tab bar is showing it should work. – David H Jul 24 '12 at 13:09
2

@Meno Thanks buddy. It worked for me.

Just copy paste

[[tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"2"]; 

in viewDidLoad.

Rob
  • 4,927
  • 12
  • 49
  • 54
Gugan
  • 1,625
  • 2
  • 27
  • 65