How to set tabBarItem's badge from AppDelegate.m (tabBarView is not root View) [duplicate]

拟墨画扇 提交于 2019-12-22 10:49:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!