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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 10:46:11
thing.navigationItem.title = sub.title;

or in StoriesViewController.m file in viewDidLoad method:

self.navigationItem.title = sub.title

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.

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";
[thing setTitle: sub.title];
Ryan Sullivan

The solution was thing.navigationItem.title = @"title"; however it turns out that subclassing a UINavigationController broke it somehow and my solution to that was to use a category rather than a subclass

If your UIViewController is part of a UITabController, then you can do:

self.tabBarController.title = sub.title;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!