shoughton123
shoughton123

Reputation: 4251

UINavigationController within a UITabBarController, setting the navigation controller title

In my app I have a UINavigationController within a UITabBarController. Everything is working fine, however I can't set the title for the navigation controller. I have tried several different methods and googled around but there seems to be no solution for this problem.

Any Help would be greatly appreciated.

Thankyou in advance.

Sam

Upvotes: 16

Views: 8604

Answers (5)

shoughton123
shoughton123

Reputation: 4251

I found the answer in the end, it was:

self.tabBarController.navigationItem.title = @"title";

Upvotes: 40

Balram Tiwari
Balram Tiwari

Reputation: 5667

The self.title always sets the tile of the rootViewController object of your ViewController.

If you have your viewController & the rootViewController is UINavigationController, then self.title will set the title of UINavigationController.

Similarly, if you have initialized a UITabViewController with the UIViewController objects, then the self.title will applies on the title corresponding button at index, on UITabBar of UITabViewController.

Hence said that, self.title applies on the object holding the viewController.

Upvotes: 0

Gobot
Gobot

Reputation: 2480

Look at the UIViewController class in the docs. There is a property pointing to the UINavigationItem which then has a property pointing to the title. If the pointer to the navigation item returns nil, you must have not wired the controllers together correctly.

Upvotes: 0

pmk
pmk

Reputation: 1897

For me the following works fine:

Initiate the controllers in appDelegateDidFinishLaunching:Method:

UINavigationController *navContr1;
UINavigationController *navContr2;

UIViewController *viewController1, *viewController2;

viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];



navContr1       = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
navContr2       = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];


self.tabBarController = [[[UITabBarController alloc] init] autorelease];
//self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navContr1, navContr2, nil];

self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

with this done, in your different viewControllers initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil - Method you can change the title with the following line:

self.title = @"Your Title";

Good Luck.

Upvotes: 1

Ugur Kumru
Ugur Kumru

Reputation: 986

self.navigationItem.title = @"Your Title"

Works for every case.

Upvotes: 4

Related Questions