Navbar title?

Discussion in 'iOS Programming' started by UberMonjie, Feb 17, 2009.

  1. macrumors newbie

    Joined:
    Feb 11, 2009
    #1
    I am having trouble setting a title for my navbar.

    Code:
    - (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!
     
  2. macrumors newbie

    Joined:
    Nov 25, 2008
    #2
    I think you should set the nav's title in its root viewcontroller . the code is like :
    self.navigationItem.title = @"Some text ";
     
  3. thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #3
    Hmmm...

    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!
     
  4. macrumors newbie

    Joined:
    Nov 25, 2008
    #4
    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 ";
     
  5. macrumors newbie

    Joined:
    Nov 25, 2008
    #5
    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 .
     
  6. thread starter macrumors newbie

    Joined:
    Feb 11, 2009
    #6
    Ahhh...

    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!
     

Share This Page