Reputation: 4251
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
Reputation: 4251
I found the answer in the end, it was:
self.tabBarController.navigationItem.title = @"title";
Upvotes: 40
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
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
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