Monday, June 10, 2013

UITabBarItem setBadgeValue example in Objective C (iOS).


UITabBarItem setBadgeValue

Text that is displayed in the upper-right corner of the item with a surrounding red oval.

@property(nonatomic, copy) NSString *badgeValue

Discussion of [UITabBarItem setBadgeValue]
The default value is nil.

UITabBarItem setBadgeValue example.
[[self navigationController] tabBarItem].badgeValue = @"3";

Or this:

[[self.tabBarController.tabBar.items objectAtIndex:2] setBadgeValue:[NSString stringWithFormat:@"%d",[UIApplication sharedApplication].applicationIconBadgeNumber]];

Example of [UITabBarItem setBadgeValue].
@implementation SomeController
...
-(void)increaseBadgeValue{
    NSString *oldVal = self.tabBarItem.badgeValue;
    self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",[oldVal intValue]+1];
}
....
@end

UITabBarItem setBadgeValue example.
InBox *inBox = [[InBox alloc] initWithNibName:nil bundle:nil];
    UINavigationController *inBoxviewcontroller= [[UINavigationController alloc] initWithRootViewController:inBox];
    tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
    tabBarController.viewControllers = [NSArray arrayWithObjects:inBoxviewcontroller,2ndViewController, 3rdViewController, 4thViewController, nil]
    inBoxviewcontroller.tabBarItem.image = [UIImage imageNamed:@\"tabbar_inbox.png\"];
    inBoxviewcontroller.title = @\"InBox\";
     [inBoxviewcontroller.tabBarItem setBadgeValue:@\"2\"];
    [inBoxviewcontroller release];

End of UITabBarItem setBadgeValue example article.