如何正确设置 UIViewController 的 navigationController 标题?
我正在尝试以下代码:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
或者在 StoriesViewController.m 文件中的
viewDidLoad
方法中:or in StoriesViewController.m file in
viewDidLoad
method:您可能正在推送 UITabBarController 而不是常规视图控制器。在这种情况下,navigationItem 将显示当前控制器(选项卡栏控制器)的标题,即使您看到另一个视图也是如此。您必须更改选项卡栏控制器的标题才能更改上面显示的文本。
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.
标题需要在 viewDidLoad 中(或之后)设置,因此如果您想从创建 StoriesViewController 的另一个类进行设置。
这是因为在
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.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.navigationItem.title = @"title";
但事实证明,对UINavigationController
进行子类化以某种方式破坏了它,我的解决方案是使用类别而不是比子类The solution was
thing.navigationItem.title = @"title";
however it turns out that subclassing aUINavigationController
broke it somehow and my solution to that was to use a category rather than a subclass如果您的 UIViewController 是 UITabController 的一部分,那么您可以执行以下操作:
If your
UIViewController
is part of aUITabController
, then you can do: