0

我正在使用以下代码从我的 App 委托中设置徽章

- (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 更新 Badge 值的最佳方法是什么?

4

1 回答 1

0

使用 tabBarItem:

[self.tabBarItem setBadgeValue:value];
于 2012-09-07T10:41:56.500 回答