iOS导航栏标题不起作用

时间:2016-03-15 19:13:47

标签: ios uiviewcontroller uinavigationcontroller uinavigationbar uinavigationitem

在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
}
然而,标题仍然是#34; Dog"在导航栏中。为什么是这样?有没有更好的方法来替换根视图控制器,以便我可以在导航栏中获得正确的标题?

1 个答案:

答案 0 :(得分:0)

我认为您的代码部分存在问题

 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