0

I am using the following code to set a badge from my App delegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

sleep(2);

    // Set the tab bar controller as the window's root view controller and display.
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

//Count the news pending and show the badge
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"datatesting.plist"];
NSFileManager *nfm = [[NSFileManager alloc] init];
if([nfm fileExistsAtPath:path])
{        
    // if file exists, get its contents, add more entries
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
    //Get the number of badge you have to show        
    int num;
    num = [array count];
    NSString *numerodeexp = [NSString stringWithFormat:@"%d",[array count]];
    [[[[[self tabBarController] tabBar] items] objectAtIndex:2] setBadgeValue:numerodeexp];

} 
else {
    [[[[[self tabBarController] tabBar] items] objectAtIndex:2] setBadgeValue:@"0"];
}


return YES;
}

I will like to update the Value from one of my view controller and I am not able to update it with this code :

    [[[[[self tabBarController] tabBar] items] objectAtIndex:2] setBadgeValue:@"0"];

What is the best way to update the Badge value from an ViewController ?

Ben
  • 1,031
  • 3
  • 17
  • 31

1 Answers1

0

Use the tabBarItem:

[self.tabBarItem setBadgeValue:value];
andreamazz
  • 4,256
  • 1
  • 20
  • 36