Reputation: 277
I am new to iPhone development... Now I am trying to build an application in which i am creating UITabbarController
programmatically like below:
UITabBarController *tabbar = [[UITabBarController alloc] init];
firstViewController *firstView = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
UINavigationController *tabItemOne = [[[UINavigationController alloc] initWithRootViewController: firstView] autorelease];
secondViewController *secondView = [[secondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
UINavigationController *tabItemTwo = [[[UINavigationController alloc] initWithRootViewController: settings] autorelease];
tabbar.viewControllers = [NSArray arrayWithObjects:tabItemOne, tabItemTwo,nil];
tabbar.view.frame = CGRectMake(0,0,320,460);
[self.view insertSubview:tabbar.view belowSubview: firstView.view];
[self presentModalViewController:tabbar animated:NO];
In this, how can I add titles to tabbar
and those controllers.
I have tried:
firstView.title = @"First View";
and
tabItemOne.title = @"First View";
But these two don't work.. How do I accomplish this?
Upvotes: 2
Views: 4239
Reputation: 19884
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"First View" image:[UIImage imageNamed:@"First View.png"] tag:0];
[tabItemOne setTabBarItem:item];
[item release];
If I'm correct.
Upvotes: 0
Reputation: 11314
UITabBarItem *item= [[[[appDelegate tabBarController]tabBar]items ]objectAtIndex:0];
//item.image=[UIImage imageNamed:@"hometab.png"];
item.title=@"First View";
item= [[[[appDelegate tabBarController]tabBar]items ]objectAtIndex:1];
//item.image=[UIImage imageNamed:@"hometab.png"];
item.title=@"Second View";
Upvotes: 0
Reputation: 51374
Setting aViewController.title sets the title for both the navigationItem and the tabBarItem. If you want the navigationItem and the tabBarItem to have different title, do this,
// First set the view controller's title
aViewController.title = @"First View Tab";
// Then set navigationItem's title
aViewController.navigationItem.title = @"First View";
Upvotes: 3
Reputation: 17918
To set the label for your controller's tab, do this:
tabItemOne.tabBarItem.title = @"First View";
(By the way, you may want to reconsider your variable names. They're pretty confusing. tabbar sounds like it should be an instance of the UITabBar, when it's actually a UITabBarController. firstView and secondView sound like instances of UIView when they're actually instances of a UIViewController subclass. tabItemOne and tabItemTwo sound like instances of UITabBarItem, when they're actually instances of UINavigationController.)
Upvotes: 0