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

I have a UITabBarController,

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

The tabBar item's created in the ib.

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



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.

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

[self.tabBarItem setBadgeValue:badgeValue];

If there are three tabs in your app First, Second and Third

Tab index starts from 0

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

If you want to set badge value 5 in Second tab

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

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

Also you can clear/remove badge value passing nil for spacific tab

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

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

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

Here is the sample code for it Tabbar with Badge Value

Need Your Help

Ruby on Rails : how to implement Cancel button in form_tag

ruby-on-rails ruby forms

I have a basic form using the form_tag helper working fine, but I want to add a cancel button, what is the syntax for doing this? I want the cancel button to appear as a button, not a link, then t...