【问题标题】:How do I correctly set a UIViewController's navigationController title?如何正确设置 UIViewController 的 navigationController 标题?
【发布时间】:2011-03-23 21:25:30
【问题描述】:

我正在尝试以下代码:

- (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 的标题。

【问题讨论】:

标签: iphone ios uinavigationcontroller uitabbarcontroller


【解决方案1】:
thing.navigationItem.title = sub.title;

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

self.navigationItem.title = sub.title

【讨论】:

  • thing.navigationItem.title 不起作用,我在推送 ViewController 之前和之后都试过了。
  • 在控制器的 viewDidLoad 方法中设置
  • 嗨,我试过 self.navigationController.title = @"Registration";有什么不同?它不起作用
  • 在 xcode 9 和 ios 11 在 swift 4 中显示明确使用标题的错误
【解决方案2】:

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

self.tabBarController.title = @"Your title";

【讨论】:

    【解决方案3】:

    标题需要在viewDidLoad 中(或之后)设置,所以如果你想从另一个正在创建 StoriesViewController 的类中设置。

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

    这是因为在viewDidLoad 之前,outlet 不会初始化为它们的视图对应物。

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

    【讨论】:

      【解决方案4】:
      [thing setTitle: sub.title];
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          如果您的UIViewControllerUITabController 的一部分,那么您可以这样做:

          self.tabBarController.title = sub.title;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-03-12
            • 2010-12-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多