How do I correctly set a UIViewController's navigationController title?

18,175

Solution 1

thing.navigationItem.title = sub.title;

or in StoriesViewController.m file in viewDidLoad method:

self.navigationItem.title = sub.title

Solution 2

It's possible you're pushing a UITabBarController instead of your regular view controller. In this case, the navigationItem will display the title of the current controller, the tab bar controller, even though you see another view. You would have to change the tab bar controller's title to change the text displayed above.

self.tabBarController.title = @"Your title";

Solution 3

The title needs to be set in (or after) viewDidLoad, so if you want to set from another class that is creating StoriesViewController.

  1. Make another property and have that class set it.
  2. In StoriesViewController's viewDidLoad, set the title from that property.

This is because outlets aren't initialized to their view counterparts until viewDidLoad.

It looks like StoriesViewController is holding on to sub (does the initWithThing set a property to it?) If so, just set the title in viewDidLoad to the Thing property's title.

Share:
18,175
Ryan Sullivan
Author by

Ryan Sullivan

Updated on October 19, 2022

Comments

  • Ryan Sullivan
    Ryan Sullivan 8 months

    I am trying the following code:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     Thing *sub = [[subscriptions objectAtIndex:indexPath.row] retain];
     StoriesViewController *thing = [[StoriesViewController alloc] initWithThing:sub];
     thing.navigationController.title = sub.title;
     [self.navigationController pushViewController:thing animated:YES];
     [thing release];
     [sub release];
    
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    

    I thought this is how you correctly set the title for pushing the controller. I tried thing.title however that was setting the TabBarItem's title instead.