views:

46

answers:

3

I have the following code:

tabBarViewController = [[TabBarViewController alloc] init];
mvc = [[MapViewController alloc] init];
tvc = [[TableViewController alloc] init];
tabBarViewController.viewControllers = [NSArray arrayWithObjects: tvc, mvc, nil];

However, when opening up in the simulator, I don't see any title or badge for these two UITabBar. How can I assign title and badge to these two ?

A: 

[tabBarViewController.tabBar.items objectAtIndex:0].badgeValue = @"1";

TabBarViewController has a property to its UITabBar. The UITabBar has the list of UITabBarItem that have the badge

Benoît
+2  A: 

You must set each child view controller's tabBarItem property:

MapViewController* mvc = [[MapViewController alloc] init];
UIImage* mapIcon = [UIImage imageNamed:@"mapIcon.png"];
mvc.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"Map" image:mapIcon tag:0] autorelease];
mvc.tabBarItem.badgeValue = @"abc";

TableViewController* tvc = [[TableViewController alloc] init];
tvc.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0] autorelease];
mvc.tabBarItem.badgeValue = @"100";
Paul Scott
A: 

Hi,why not you use this code in tvc, mvc - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization

self.tabBarItem = [ [ UITabBarItem alloc ] initWithTitle:@"YOUR TITLE" image:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"IMAGE" ofType:@"png"]] tag:1 ];

}
return self;

}

sudesh