2012-09-07 30 views
0

我使用下面的代码从我的应用程序委托设置徽章IOS更新BadgeValue

- (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; 
} 

我会想从我的视图控制器的一个更新的价值,我无法用它来更新它代码:

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

从ViewController更新徽章值的最佳方法是什么?

回答

0

使用tabBarItem:

[self.tabBarItem setBadgeValue:value];