如何正确设置 UIViewController 的 navigationController 标题?

发布于 2024-09-12 07:52:03 字数 570 浏览 6 评论 0原文

我正在尝试以下代码:

- (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];
}

我认为这就是正确设置推动控制器的标题的方法。我尝试了 thing.title 但是它设置了 TabBarItem 的标题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

难理解 2024-09-19 07:52:05
thing.navigationItem.title = sub.title;

或者在 StoriesViewController.m 文件中的 viewDidLoad 方法中:

self.navigationItem.title = sub.title
thing.navigationItem.title = sub.title;

or in StoriesViewController.m file in viewDidLoad method:

self.navigationItem.title = sub.title
世界和平 2024-09-19 07:52:05

您可能正在推送 UITabBarController 而不是常规视图控制器。在这种情况下,navigationItem 将显示当前控制器(选项卡栏控制器)的标题,即使您看到另一个视图也是如此。您必须更改选项卡栏控制器的标题才能更改上面显示的文本。

self.tabBarController.title = @"Your 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";
吖咩 2024-09-19 07:52:05

标题需要在 viewDidLoad 中(或之后)设置,因此如果您想从创建 StoriesViewController 的另一个类进行设置。

  1. 创建另一个属性并让该类设置它。
  2. 在 StoriesViewController 的 viewDidLoad 中,从该属性设置标题。

这是因为在 viewDidLoad 之前,插座不会初始化为其对应的视图。

看起来 StoriesViewController 正在保留 sub(initWithThing 是否为其设置了属性?)如果是这样,只需将 viewDidLoad 中的标题设置为 Thing 属性的标题即可。

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.

仅冇旳回忆 2024-09-19 07:52:05
[thing setTitle: sub.title];
[thing setTitle: sub.title];
谁与争疯 2024-09-19 07:52:05

解决方案是 thing.navigationItem.title = @"title"; 但事实证明,对 UINavigationController 进行子类化以某种方式破坏了它,我的解决方案是使用类别而不是比子类

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

柠檬 2024-09-19 07:52:05

如果您的 UIViewController 是 UITabController 的一部分,那么您可以执行以下操作:

self.tabBarController.title = sub.title;

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

self.tabBarController.title = sub.title;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文