如何从 AppDelegate.m 设置 tabBarItem 的徽章(tabBarView 不是根视图) [英] How to set tabBarItem's badge from AppDelegate.m (tabBarView is not root View)

查看:27
本文介绍了如何从 AppDelegate.m 设置 tabBarItem 的徽章(tabBarView 不是根视图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can change badge value in AppDelegate.m by following code:

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

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

Only if the tabBarViewController is window's root view controller.

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

However, in my new program, tabBarController is not root view.

我尝试了以下代码:

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

这根本行不通.tabBarItem 不显示任何徽章.

It just won't work. tabBarItem doesn't show any badge.

编辑-

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

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");
}

答案是肯定的.

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

2013-03-01 11:57:00.600 Reporting[14854:907] tabControllerhas been created

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

推荐答案

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

can you try this code Mr. Robert

       [[[[[self tabBarController] tabBar] items] objectAtIndex:0] setBadgeValue:[NSString stringWithFormat:@"%d", notificationCount ]];

这篇关于如何从 AppDelegate.m 设置 tabBarItem 的徽章(tabBarView 不是根视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆