【问题标题】:iOS Navigation Bar title not workingiOS导航栏标题不起作用
【发布时间】:2016-03-15 19:13:47
【问题描述】:

在 DogViewController 中说,它是故事板中导航控制器的根视图控制器,我有这个:

- (void)viewDidLoad {
    [super viewDidLoad];

    if (someConditionWhereIWantCatInsteadOfDog) {
        CatViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Cat"];
        self.navigationController.viewControllers = @[vc];

        return;
    }
    ...
}

然后在 CatViewController 我有这个:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.title = @"Cat"; //this definitely gets called, but has no effect
}

但是导航栏中的标题仍然是“狗”。为什么是这样?有没有更好的方法来替换根视图控制器,以便我可以在导航栏中获得正确的标题?

【问题讨论】:

  • 试试 self.navigationController.navigationItem.title = @"hai";
  • 当你说 self.title 它指的是 UIViewController 而不是 UINavigationViewController。所以你应该使用 self.navigationController.navigationItem.title。
  • 如果您的 viewController 有您提到的 navigationBarController,它应该可以工作。您是否在 viewDidLoad 中尝试过?我会这样呈现:[self.navigationController presentViewController:vc animated:true completion:nil];

标签: ios uiviewcontroller uinavigationcontroller uinavigationbar uinavigationitem


【解决方案1】:

我认为你的这部分代码有问题

 if (someConditionWhereIWantCatInsteadOfDog) {
    CatViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Cat"];
    self.navigationController.viewControllers = @[vc];

    return;
}

要分配控制器,您必须推动它

if (someConditionWhereIWantCatInsteadOfDog) {
    CatViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Cat"];
    [self.navigationController pushViewController:vc animated:true];

    return;
}

或在情节提要中分配根视图控制器 See image in this link

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2018-03-04
    相关资源
    最近更新 更多