在我的应用中,我的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:
中设置徽章值,则表示没有徽章。
答案 0 :(得分:3)
您需要阅读UIApplicationDelegate协议以及各种状态转换。仅当手机中发生一组状态更改时,才会在启动时发生applicationDidBecomeActive转换。
答案 1 :(得分:2)
@Meno感谢好友。它对我有用。
只需复制粘贴
即可[[tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"2"];
viewDidLoad
中的。