如何从 AppDelegate.m 设置 tabBarItem 的徽章(tabBarView 不是根视图)[重复]

2024-05-02

我可以通过以下代码更改 AppDelegate.m 中的徽章值:

UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
[[tabController.viewControllers objectAtIndex:0] tabBarItem].badgeValue = @"Badge";

仅当 tabBarViewController 是窗口的根视图控制器时。

但是,在我的新程序中,tabBarController 不是根视图。

我尝试过以下代码:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UITabBarController *tabController = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:@"tabBarController"] ;
[[tabController.viewControllers objectAtIndex:0] tabBarItem].badgeValue = @"Badge";

它就是行不通。 tabBarItem 不显示任何徽章。

EDIT-

我添加了以下代码来检查 tabController 是否已创建,答案是肯定的。

if (tabController) {
     NSLog(@"tabController has been created ");
}else {
     NSLog(@"You have a null reference - The Object hasn't been created yet");
}

答案是肯定的。

2013-03-01 11:57:00.600 报告[14854:907] tabController 已 已创建

但是,预期的徽章仍然没有出现。


罗伯特先生,你能试试这个代码吗

       [[[[[self tabBarController] tabBar] items] objectAtIndex:0] setBadgeValue:[NSString stringWithFormat:@"%d", notificationCount ]];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 AppDelegate.m 设置 tabBarItem 的徽章(tabBarView 不是根视图)[重复] 的相关文章

随机推荐