Sh0ckValu3 Sh0ckValu3 - 1 month ago 8
iOS Question

Change title of navbar based on which tab is selected?

I have an iOS app where there is a navigation controller as the root controller but at one part there is a tab bar to select between views in one of the nav bars. It looks similar to the iTunes app (navigation bar on top, tab bar on the bottom). I want the title of my navigation bar to change based on which tab is selected. I have two separate controller files for each tab. Here is what I have tried to use in each so far to fix this to no avail:

self.navigationItem.title = @"Title";
self.navigationController.navigationItem.title = @"title";
[self.navigationController setTitle:@"Live"];
[self setTitle:@"Top Title"];


How do I change the NavBar title based on which tab is pressed?

Answer

You change the title of the bar in the view controller that is currently being displayed.

So for example, in view controller A that you're showing in the tab controller, you might add:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self setTitle:@"A"];
    self.tabBarController.navigationItem.title = @"A";
}

Same goes for B, C, etc.