开发者

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

开发者 https://www.devze.com 2023-01-09 23:30 出处:网络
I am trying the following code: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

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.


thing.navigationItem.title = sub.title;

or in StoriesViewController.m file in viewDidLoad method:

self.navigationItem.title = sub.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";


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.


[thing setTitle: sub.title];


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

精彩评论

暂无评论...
验证码 换一张
取 消