PDA

View Full Version : Navbar title?




UberMonjie
Feb 17, 2009, 10:36 AM
I am having trouble setting a title for my navbar.


- (void)applicationDidFinishLaunching:(UIApplication *)application {

UITabBarController *tabBarController = [[UITabBarController alloc] init];

// Init view controllers
GabViewController *gvc = [[GabViewController alloc] init];
VoteViewController *vvc = [[VoteViewController alloc] init];

// Navbar controller, init with root view from Gab View
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:gvc];
nav.navigationBar.barStyle = UIBarStyleBlackOpaque;
nav.navigationItem.title = @"Sayonara Chat";
nav.title = @"Sayonara Chat";


// Tab bar titles and icons
nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Gab" image:[UIImage imageNamed:@"icon_root.png"] tag:0];
vvc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Vote" image:[UIImage imageNamed:@"icon_another.png"] tag:1];

// Add to the tab bar, animated
[tabBarController setViewControllers:[NSArray arrayWithObjects:nav,vvc, nil] animated:YES];

[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}


You can see I am trying two different ways to set the title, neither works?! Also, I can't add buttons to the navbar? I CAN set the barStyle just fine! What gives?

Any thoughts are much appreciated!



mars.tsang
Feb 17, 2009, 12:53 PM
I think you should set the nav's title in its root viewcontroller . the code is like :
self.navigationItem.title = @"Some text ";

UberMonjie
Feb 17, 2009, 11:50 PM
Thanks, although I am little confused, in this case:

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:gvc];

do I need to add a "RootViewController"? or is gvc my rootview controller in this case? I can't seem to add a title to this thing either way?!

Thanks for trying to help!

mars.tsang
Feb 18, 2009, 04:04 AM
Thanks, although I am little confused, in this case:

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:gvc];

do I need to add a "RootViewController"? or is gvc my rootview controller in this case? I can't seem to add a title to this thing either way?!

Thanks for trying to help!

so gvc is the first viewcontroller of your navigationcontoller ,which mean gvc is the first one in the nav's view controller's stack .
So somewhere in your gvc's source code ,you apply the code:
self.navgationItem.title = @"something ";

mars.tsang
Feb 18, 2009, 04:08 AM
there's also another way to set the nav's title too.
if you set the title property for your gvc ,like self.title = @"some title" and when you set the gvc to be the nav's root view controller or push it into the nav , then the nav's title will be the same as the gvc's .

UberMonjie
Feb 18, 2009, 10:21 AM
Oh, I get it now... Thanks!

I was trying to set the title of the navbar controller FROM the gvc code. I didn't understand the title belongs to the controller that is pushed on to the nav at the time...

Many thanks friend!