slonkar
slonkar

Reputation: 4085

UITabBarController Does not shows tabbarItems name

I am creating app based on UTabbarController. I have creates that tab bar programmatically. Everything is running fine except I can not see the tabBatItem title. I have initialized everything properly, but when application launches all I can see is the first tabbar title. but if I select 2nd tabbaritem or so on I can see their names. I don't know whats going wrong here. Here is my code. Please let me know if I made any mistake.

Thanks.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    HomeViewController *viewController1 = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController*navController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navController1.title=@"Home";
[viewController1 release];

TrainerTableViewController *viewController2 = [[TrainerTableViewController alloc] initWithNibName:@"TrainerTableViewController" bundle:nil];
UINavigationController*navController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navController1.title=@"Trainer";
[viewController2 release];

SettingsTableViewController *viewController8 = [[[SettingsTableViewController alloc] initWithNibName:@"SettingsTableViewController" bundle:nil] autorelease];
UINavigationController*navController8=[[[UINavigationController alloc]initWithRootViewController:viewController8]autorelease];
navController1.title=@"Settings";

AboutUsViewController *viewController9 = [[[AboutUsViewController alloc] initWithNibName:@"AboutUsViewController" bundle:nil] autorelease];
UINavigationController*navController9=[[[UINavigationController alloc]initWithRootViewController:viewController9]autorelease];
navController1.title=@"About Us";


self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2,navController8, navController9, nil];

[navController1 release];
[navController2 release];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

Upvotes: 0

Views: 2446

Answers (4)

aks.knit1108
aks.knit1108

Reputation: 1305

The best way to solve this problem is to set title of viewController not of navigationController

viewController1.tabBarItem.title = @"CohesiveSelf";

Do this for all tabs.

Upvotes: 1

superjessi
superjessi

Reputation: 1790

You are actually setting the title for the NavigationControllers. Also, watch that your code sets the title of navController1 multiple times, rather than setting it for the others.

You can set the titles for your tabBar by setting up tabBarItems for each controller.

You have the option of subclassing, or just including this in the application:didFinishLaunchingWithOptions: method in your AppDelegate.

Here's an example:

UITabBarItem *tbi1 = [navController1 tabBarItem];
[tbi1 setTitle:@"Home"];

UIImage *i1 = [UIImage imageNamed:@"hometabicon.png"];
[tbi1 setImage:i1];

This will set the title of tab1 to 'Home' and will set the tabBar icon to a file named 'hometabicon.png'.

You can repeat the same pattern for each of the other tabs.

Upvotes: 0

Flippinjoe
Flippinjoe

Reputation: 61

You're only setting the title for navController1. For each nab controller you create you need to set the title for that one.

Try this:

CHANGE: navController1.title=@"Trainer"; TO navController2.title=@"Trainer";

CHANGE: navController1.title=@"Settings"; TO navController8.title=@"Settings";

CHANGE: navController1.title=@"About Us"; TO navController9.title=@"About Us";

Also not you are not releasing navController8 or 9 which will cause in a memory leak

Upvotes: 0

Louie
Louie

Reputation: 5940

You can do this by including the code below inside the .m file of the view controller for each tab bar item. The code also includes how to change the image on the tab bar.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
        self.title = @"Apply Now";
        self.tabBarItem.image = [UIImage imageNamed:@"tbApplyNow.png"];
    }
    return self;
}

Upvotes: 3

Related Questions