问题
I can change badge value in AppDelegate.m by following code:
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
[[tabController.viewControllers objectAtIndex:0] tabBarItem].badgeValue = @"Badge";
Only if the tabBarViewController is window's root view controller.
However, in my new program, tabBarController is not root view.
I have tried following code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *tabController = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:@"tabBarController"] ;
[[tabController.viewControllers objectAtIndex:0] tabBarItem].badgeValue = @"Badge";
It just won't work. tabBarItem doesn't show any badge.
EDIT-
I have added following code to check does tabController created or not, The answer is YES.
if (tabController) {
NSLog(@"tabController has been created ");
}else {
NSLog(@"You have a null reference - The Object hasn't been created yet");
}
The answer is YES.
2013-03-01 11:57:00.600 Reporting[14854:907] tabControllerhas been created
But, the expected badge still does not show up.
回答1:
can you try this code Mr. Robert
[[[[[self tabBarController] tabBar] items] objectAtIndex:0] setBadgeValue:[NSString stringWithFormat:@"%d", notificationCount ]];
来源:https://stackoverflow.com/questions/15129260/how-to-set-tabbaritems-badge-from-appdelegate-m-tabbarview-is-not-root-view