标签栏项目上的徽章未显示

时间:2012-07-24 10:30:51

标签: iphone ios uitabbaritem badge

在我的应用中,我的tabBar项目的徽章价值在AppDelegate.m中设置,如下所示:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UITabBarController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"]; 
    [[tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"1"];
    ...
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    ...
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UITabBarController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"]; 
    [[tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"2"];
    ...
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    ...
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UITabBarController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"]; 
    [[tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"3"];
    ...
}

问题是:徽章值始终为“1”。为什么applicationWillEnterForeground:applicationDidBecomeActive:中设置的徽章值永远不会出现?如果我没有在application: didFinishLaunchingWithOptions:中设置徽章值,则表示没有徽章。

2 个答案:

答案 0 :(得分:3)

您需要阅读UIApplicationDelegate协议以及各种状态转换。仅当手机中发生一组状态更改时,才会在启动时发生applicationDidBecomeActive转换。

答案 1 :(得分:2)

@Meno感谢好友。它对我有用。

只需复制粘贴

即可
[[tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"2"]; 
viewDidLoad中的