Search code examples
iphoneobjective-ciosuitabbarcontrollertabbarcontroller

Add titles to tabbar created programmatically


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?


Solution

  • 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";