0

In a VC that's embedded in a tabbar I do this:

- (void)viewDidLoad
{
    self.title = LS(@"Choose a soundtrack");
    self.tabBarItem.title = LS(@"Music"); // self.title would not fit into tab 

    [super viewDidLoad];

I expect the self.title to seep through into both navbar and tab and it does end up in tabbaritem (which is why I have to restore it right after).

But it does not show up in the navbar. Similarly, self.navigationItem.title setup has zero effect.

Likewise self.navigationController.title = LS(@"Choose a soundtrack"); has zero effect.

In case that matters the tabbar is pushed into navigation controller like so:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"TabbedStory" bundle:[MediaManager mediaBundle]];
SixTabViewController *stvc = [storyBoard instantiateInitialViewController];
[self.navigationController pushViewController:stvc animated:YES];

Note the excellent name I picked for the tabbar vc. The king is naked.

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
  • use self.navigationController.title = "Your title " instead of setting title of vc. – dip Jun 12 '17 at 12:04
  • Looks like your VC in a fact is first embedded into navigation controller, which is embedded into tab bar controller... Would be good if you clarify your view controllers hierarchy and attach screenshot. – Varrry Jun 12 '17 at 12:09
  • Tabbar controller is pushed into navstack. The VC in question represents one of the tabs in the tabbar. – Anton Tropashko Jun 12 '17 at 12:11

2 Answers2

1

If your hierarchy is NavController->TabBarController->ViewController then it won't work; view controller's title is displayed in navigation bar of navigation controller if only it's embedded directly. But in your case navigation controller displays tab bar controller and tries do display it's title (which is absent).

Commonly another hierarchy is used: embed navigation controller into tab bar controller, and then view controller's title will be displayed in navigation controller's navbar.

If this isn't possible for you, you can change tab bar controller's title every time you switch page in tab bar.

Varrry
  • 2,647
  • 1
  • 13
  • 27
0

The superugly hack is to have the containing tabbar controller set the navigationItem titles like so:

-(void)prepareForIndex:(NSUInteger)i
{
...
switch(i) {
    case 2:
        self.navigationItem.title = LS(@"Choose a soundtrack");
}
}

-(void)setSelectedIndex:(NSUInteger)i
{
[self prepareForIndex:i];
[super setSelectedIndex:i];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    [self prepareForIndex:self.selectedIndex];
}

The clue was here: Why does UICollectionView log an error when the cells are fullscreen?

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66