Monday, June 10, 2013

UITabBarItem badgeValue example in Objective C (iOS).


UITabBarItem badgeValue

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 badgeValue]
The default value is nil.

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

Or this:

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

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

UITabBarItem badgeValue 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.badgeValue = @\"2\";
    [inBoxviewcontroller release];

End of UITabBarItem badgeValue example article.