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

ビューコントローラーの1つから値を更新したいのですが、このコードでは更新できません:

    [[[[[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 に答える