Question or problem in the Swift programming language:
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
How to solve the problem:
Solution 1:
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. In your case, the tabBarController has two viewControllers, which are:
- The team navigation controller
- 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"
Solution 2:
Try this
var cart: UITabBarItem = super.tabBarController.viewControllers[yourIndex].tabBarItem() cart.badgeValue = "3"
Solution 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. So in order to access it, you can subclass your UINavigationController and make the TabBarItem a property of that class. Then you can access the the TabBarItem via that property. For Example:
@interface MyCustomNavController : UINavigationController @property(weak, nonatomic)IBOutlet UITabBarItem *theTabBarItem; @end
and from your TableController access it with the following:
MyCustomNavController* navController=(MyCustomNavController*)self.navigationController; [email protected]"3";