objective-c iphone 代码 - 从 viewController 更改 tabBar 徽章值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1949475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:39:20  来源:igfitidea点击:

iphone code - change the tabBar badge value from the viewController's

iphoneobjective-c

提问by omri

I have a UITabBarController,

我有一个UITabBarController

How can I create/update the badge valueof the tabBar item from my viewController?

如何badge value从我的viewController?

The tabBaritem's created in the ib.

tabBar项目是在 ib 中创建的。

I connected the tabBar item to the controller using an IBOutlet UITabBar *tabBar.

我使用IBOutlet UITabBar *tabBar.

thanks.

谢谢。

回答by Matt Long

If your viewcontroller already has a tab bar controller associate with it, you can just drill down to the tab bar item and set its badge, like this:

如果你的视图控制器已经有一个标签栏控制器与之关联,你可以深入到标签栏项目并设置它的徽章,如下所示:

[[[[[self tabBarController] tabBar] items] 
                   objectAtIndex:tabIndex] setBadgeValue:badgeValueString];

where tabIndex is the index of the tab item you want to set and badgeValueString is the string value you want to set on the tab.

其中 tabIndex 是您要设置的选项卡项的索引,而badgeValueString 是您要在选项卡上设置的字符串值。

回答by Matt Long

Use the tabBarItem instance of your view controller to access the tab bar item directly instead of drilling down to it.

使用视图控制器的 tabBarItem 实例直接访问选项卡栏项目,而不是深入查看它。

[self.tabBarItem setBadgeValue:badgeValue];

回答by MaxEcho

If there are three tabs in your app First, Secondand Third

如果您的应用程序中有三个选项卡FirstSecond并且Third

Tab index starts from 0

标签索引从 0 开始

First  tab index 0
Second tab index 1
Third  tab index 2

If you want to set badge value 5 in Secondtab

如果要在Second选项卡中设置徽章值 5

So pass 1(tab index of Second tab) for objectAtIndex:1and pass 5(badge value) for setBadgeValue:@"5"

所以传递 1(第二个标签的标签索引)objectAtIndex:1并传递 5(徽章值)setBadgeValue:@"5"

[[self.tabBarController.tabBar.items objectAtIndex:1] setBadgeValue:@"5"];

Also you can clear/remove badge value passing nilfor spacific tab

您也可以清除/删除nil为空间选项卡传递的徽章值

[[self.tabBarController.tabBar.items objectAtIndex:1] setBadgeValue:nil];

回答by herock

        int indexICareAbout = 2;
        NSString *badgeValue = @"10";

        [[[[[self tabBarController] viewControllers]
        objectAtIndex: indexICareAbout] tabBarItem] setBadgeValue:badgeValue];