3

So here's how I set up things in my app. I have a UINavigationController which has a UITabBarController as the rootViewController. The rootViewController has 3 tabs in it. I have tried many different ways to set the title, ranging from:

self.title = @"title";

self.navigationController.title = @"title";

self.navigationItem.title = @"title";

and none of it works

Why is this?

UPDATE:

for some weird reason self.parentViewController.title = @"Map"; actually worked....

adit
  • 32,574
  • 72
  • 229
  • 373
  • see if http://stackoverflow.com/questions/6644555/uitabbaritem-title-vs-uinavigationcontroller-title solves your problem. – Manny Sep 05 '11 at 07:46

4 Answers4

3

I am guessing you want is to set the text that appears in the center of the navigation bar. If so, navigationItem is definitely not what you want. That's for setting buttons in the navigation bar.

The less obvious part of setting the title is that the text that will appear in the navigation bar is the title of the ViewController that is on top. In other words, the last ViewController that was pushed. So what you want to do is to set your self.title in your ViewContoller. You can do it ViewContoller's init or viewDidLoad.

rjgonzo
  • 1,761
  • 1
  • 16
  • 29
  • For this to work, be sure your ViewController is connected in Interface Builder's Identity Inspector via the Custom Class attribute. – dev4life May 02 '17 at 07:16
2

I tried NSLogging every title attribute from the navigation bar, eventually for me it worked to set the navBar.topItem.title attribute. Of course, navBar is a reference to the UINavigationBar in my IB.

0

self.title should be what you use.

1.) When are you trying to change the title? for example, when pressing a tab button?

2.) Where are you placing this code? This should be in the viewDidLoad method

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • yes I did put it in viewDidLoad as self.title = @"Map";, it doesn't work. I am trying to show a title when the view initially shows up.. otherwise there's no title, as of now it just shows Root View Controller as the title – adit Sep 05 '11 at 06:40
  • 1
    `self.title` should be set in `init` since it could be used before the view is loaded. Like in a tab bar, the title of the tab is used even if the view is loaded only when you change tab. – gcamp Sep 05 '11 at 06:44
  • and yes I am using initWithTabBar, I did try self.title there, no effect – adit Sep 05 '11 at 06:46
  • `initWithTabBar` is your issue then, use `.parentViewController.title`. – WrightsCS Sep 05 '11 at 06:48
  • why is initWithTabBar is an issue? – adit Sep 05 '11 at 16:24
-1

Here it comes....

You must write following line to add tabbarcontroller in appdelegate..

mTabBar = [[UITabBarController alloc] init];
    NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:3];


    TSDetailTaskController *mTSDetailTaskController = [[TSDetailTaskController alloc]initWithNibName:@"TSDetailTaskController" bundle:nil];
    UINavigationController *mTaskNavBar=[[UINavigationController alloc]initWithRootViewController:mTSDetailTaskController];
    mTaskNavBar.tabBarItem.title=@"Task List";
    mTaskNavBar.tabBarItem.image =[UIImage imageNamed:@"glyphicons_114_list.png"];
    [mTSDetailTaskController release];

    mTSSearchController=[[TSSearchController alloc]initWithNibName:@"TSSearchController" bundle:nil];
    UINavigationController *mSearchNavBar=[[UINavigationController alloc]initWithRootViewController:mTSSearchController];
    mSearchNavBar.title=@"Search";
    mSearchNavBar.tabBarItem.image=[UIImage imageNamed:@"glyphicons_009_search.png"];
    [mTSSearchController release];

    TSSettingController *mTSSettingController = [[TSSettingController alloc]initWithNibName:@"TSSettingController" bundle:nil];
    UINavigationController *mSettingNavBar=[[UINavigationController alloc]initWithRootViewController:mTSSettingController];
    mSettingNavBar.tabBarItem.title=@"Setting";
    mSettingNavBar.tabBarItem.image=[UIImage imageNamed:@"glyphicons_280_settings.png"];
    [mTSSettingController release];


    [localViewControllersArray addObject:mTaskNavBar];  
    [localViewControllersArray addObject:mSearchNavBar];
    [localViewControllersArray addObject:mSettingNavBar];

    [mTaskNavBar release];
    [mSearchNavBar release];
    [mSettingNavBar release];


    mTabBar.viewControllers = localViewControllersArray;
    mTabBar.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);

    [localViewControllersArray release];


    [window addSubview:mTabBar.view];
    [self.window makeKeyAndVisible];
    return YES;

I suppose that u r creating ur project based on UINavigation controller type...

then in the "ViewDidLoad" of ur rootviewcontroller try self.title

Hope this eill help u out..

iCoder4777
  • 1,682
  • 1
  • 14
  • 35
  • why do I have to add another UINavigationController again on top of it? doing that gives me this http://imgur.com/kPQg5 – adit Sep 05 '11 at 06:43