1

在我的应用程序中,我的 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:,则那里没有显示徽章。

4

2 回答 2

3

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

于 2012-07-24T12:13:25.187 回答
2

@Meno 谢谢哥们。它对我有用。

只需复制粘贴

[[tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"2"]; 

viewDidLoad.

于 2012-11-08T14:03:39.297 回答