目录
- 1.背景
- 2.原因
- 3.如何解决
- 4.总结
1.背景
在我们的APP中,从首页付费故事-VIP故事-点击购买-购买结果界面-立刻收听-故事详情页-点击播放-播放界面-返回到VIP故事界面,导航栏消失
2.原因
- 最开始怀疑是设置
setNavigationBarHidden
没有设置或者设置的BOOL
值相反。根据这个推测,在以上流程中相关的ViewController中搜索NavigationBarHidden
,查看之后发现都是配对出现的,没有问题。但是从KSGoodDetailViewController
pop到KSVipListViewController
时,NavigationBarHidden
的值变为了1。 - 排除了没有设置或者赋值错误的问题,那就继续查看点击返回按钮时操作,发现这一块的返回不一样,如果是已购买,则pop时需要跳过购买结果界面,否则直接
popViewController
,代码如下
- (void)leftViewTapped:(KSPersonalTopBar *)topBar {
NSArray *viewControllers = self.navigationController.viewControllers;
NSInteger indexCount = viewControllers.count;
if (indexCount > 2 && [viewControllers[indexCount - 2] isKindOfClass:NSClassFromString(@"KSOrderResultViewController")]) {
UIViewController *vv = [viewControllers safeObjectAtIndex:indexCount - 3];
if (vv) {
[self.navigationController popToViewController:vv animated:YES];
}
} else {
[self.navigationController popViewControllerAnimated:YES];
}
}
将popToViewController
改为popToViewController
则没有此问题,那么用popToRootViewController
呢,则存在一样的问题,在ViewWillDisappear
方法中self.navigationController
为nil
,导致在[self.navigationController setNavigationBarHidden:NO animated:YES]
方法不执行。所以回到VIP故事界面时,导航栏不显示。
-
在Google上搜了一下,也有网友遇到此问题,其中在
Apple
工作的possen
说Not sure if this fixes your problem but I had this problem because the class that implemented the code that popped the view controller was actually getting popped. This caused my self.navigationController to be nil because it itself was getting removed.
3.如何解决
当时最初的想法,是放到viewDidLoad里面,移除self.navigationcontroller.viewcontrollers
中对应的ViewController
,但是在Stack Overflow上面看到在此处操作,容易出现ViewController
不在栈中的情况,最后还是保留在点击的时候,移除对应的ViewController
,然后将popToViewController
方法改为popViewControllerAnimated
,如下
- (void)leftViewTapped:(KSPersonalTopBar *)topBar {
NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
NSInteger indexCount = viewControllers.count;
if (indexCount > 2 && [viewControllers[indexCount - 2] isKindOfClass:NSClassFromString(@"KSOrderResultViewController")]) {
[viewControllers removeObjectAtIndex:indexCount - 2];
self.navigationController.viewControllers = [viewControllers copy];
}
[self.navigationController popViewControllerAnimated:YES];
}
4.总结
此类问题在我看来,是苹果的bug,这种bug给开发者带来极大的开发负担,也降低了用户的良好体验,为了避免此问题的发生,我在我们的工程中搜索了所有调用popToViewController
和popToRootViewController
方法的地方是否在ViewWillDisappear
调用了self.navigationController
,确保万无一失。
参考的链接
popToRootViewControllerAnimated popToViewController navigationController nil
NavigationController becomes nil after popToViewController