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

女生的网名这么多〃 提交于 2019-12-07 05:00:59

问题


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.


回答1:


thing.navigationItem.title = sub.title;

or in StoriesViewController.m file in viewDidLoad method:

self.navigationItem.title = sub.title



回答2:


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.




回答3:


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";



回答4:


[thing setTitle: sub.title];



回答5:


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




回答6:


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

self.tabBarController.title = sub.title;


来源:https://stackoverflow.com/questions/3382573/how-do-i-correctly-set-a-uiviewcontrollers-navigationcontroller-title

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