Geek Geek - 2 months ago 17
Objective-C Question

BadgeValue not getting updated on UITabBarItem

I use

UITabBar
and
UITabBarItems
with it. I can set badge value for
UITabBarItem
before assigning
tabBarItem
to
tabBar
. But my problem is that I am not able to update the badge value of
tabBarItem
.

Here is code where I am able to set badge value initially:

// array of tabBarItems
NSMutableArray * tabs = [[NSMutableArray alloc] init];

for(iterates few times)
{
[tabs addObject:[[UITabBarItem alloc] initWithTitle:firstName image:nil tag:i]];
// set tabItem's property
[(UITabBarItem *)[tabs objectAtIndex:i] setFinishedSelectedImage:[self convertImage:iconImage toSize:CGSizeMake(iconWidth, TAB_ICON_HEIGHT)] withFinishedUnselectedImage:[self convertImage:iconImage toSize:CGSizeMake(iconWidth, TAB_ICON_HEIGHT)]];
[[tabs objectAtIndex:0] setBadgeValue:[NSString stringWithFormat:@"%d", 2]];
}
// setting items of UITabBar
[self.chatTabBar setItems:tabs];


Here, I try to update the badge value. So what happens is that if I
NSLog
the new badge value it shows the updated value but I see no change in UI.

[[self.chatTabBar.items objectAtIndex:0] setBadgeValue:[NSString stringWithFormat:@"%d", 1]];

Answer

Try it:

UIViewController *carrinhoVC = [self.tabBarController.viewControllers objectAtIndex:0];

            carrinhoVC.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", 1];

carrinhoVC is the UIViewController that you want to update badge value inside TabBar.