简体   繁体   English

为什么标签栏项目的徽章没有更改

[英]why the badge of the tab bar item is not changed

I am trying to change the badge of the tab bar item but no success. 我正在尝试更改标签栏项目的徽章但没有成功。 I understood that tab bar controller is responsible of controller the tab bar. 我明白标签栏控制器负责控制器的标签栏。

However, some content in the tab bar itself can be managed by the view controller such as the badge 但是,标签栏本身中的某些内容可以由视图控制器(例如徽章)管理

in my code i tried this in view did load 在我的代码中我尝试了这个在视图中加载

self.tabBarItem.badgeValue = "3"

but nothing appears in the badge 但徽章中没有任何内容

and then I tried : 然后我试了一下:

self.tabBarController?.tabBarItem.badgeValue = "3"

which didn't work neigher, well, i know why the second code didn't work, it is the same as changing the title of a navigation controllre using the navigation controller not the navigation item. 哪个不起作用,好吧,我知道为什么第二个代码不起作用,它与使用导航控制器而不是导航项更改导航控件的标题相同。 but i don't know why the first code didn't work 但我不知道为什么第一个代码不起作用

this is the hiechy of my app, and i am doing so in the TeamsTableViewController which is the first view controller in the teams tab 这是我的应用程序的hiechy,我在TeamsTableViewController中这样做,这是团队选项卡中的第一个视图控制器

在此输入图像描述

In your scenario, It is the navigation controller that controls the tabBarItem not the TableViewController, because each TabBarController has an array of viewControllers, and each one of these viewControllers is associated with a tabBarItem. 在您的场景中,导航控制器控制tabBarItem而不是TableViewController,因为每个TabBarController都有一个viewControllers数组,并且这些viewControllers中的每一个都与tabBarItem相关联。 In your case, the tabBarController has two viewControllers, which are: 在您的情况下,tabBarController有两个viewControllers,它们是:

  1. The team navigation controller 团队导航控制器
  2. The team view controller 团队视图控制器

So the team view navigation controller is the view controller that controllers the tab bar item. 因此团队视图导航控制器是控制选项卡栏项的视图控制器。

Doing this should solve your problem 这样做可以解决您的问题

self.navigationController?.tabBarItem.badgeValue = "3"

I know this question is quite old, but unanswered. 我知道这个问题已经很老了,但没有答案。 As William states, your navigation Controller is the one that has control over the TabBarItem. 正如威廉所说,您的导航控制器是可以控制TabBarItem的控制器。 So in order to access it, you can subclass your UINavigationController and make the TabBarItem a property of that class. 因此,为了访问它,您可以继承UINavigationController并使TabBarItem成为该类的属性。 Then you can access the the TabBarItem via that property. 然后,您可以通过该属性访问TabBarItem。 For Example: 例如:

@interface MyCustomNavController : UINavigationController
@property(weak, nonatomic)IBOutlet UITabBarItem *theTabBarItem;
@end

and from your TableController access it with the following: 并从您的TableController访问它与以下内容:

MyCustomNavController* navController=(MyCustomNavController*)self.navigationController;
navController.theTabBarItem.badgeValue=@"3";

Try this 试试这个

var cart: UITabBarItem = super.tabBarController.viewControllers[yourIndex].tabBarItem()
cart.badgeValue = "3"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM