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];