此文章为引用,原文章的地址为https://blog.csdn.net/weilaizhuan/article/details/51063275
当我们做app的时候有时候会遇到这样的需求,根视图是A一个“个人信息” 页面,点击头像会push到“个人详细信息”的B页面,当我们点击某一个详细的信息进行修改时会push到“修改信息”的C页面(或者会有详细的地区选择的D、E、F等等)。修改后我们需要Pop到B视图,然而苹果给我们提供的有两种方法
1、推出到根视图控制器
- (nullableNSArray<__kindofUIViewController*> *)popToRootViewControllerAnimated:(BOOL)animated;
2、推出到指定的视图控制器
- (nullableNSArray<__kindofUIViewController*> *)popToViewController:(UIViewController*)viewController animated:(BOOL)animated;
然而当我们直接用第二种方法时系统会“崩溃”,提示
Terminating app due to uncaught exception'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'
pop推出的视图控制器并不存在,那么我们该如何使用呢?
for(UIViewController*controllerinself.navigationController.viewControllers) {
if([controller isKindOfClass:[ReviseUserInformationViewControllerclass]]) {
ReviseUserInformationViewController *revise =(ReviseUserInformationViewController *)controller;
[self.navigationController popToViewController:revise animated:YES];
}
}
压入栈了,那我们就可以在栈内查找
[self.navigationController popToRootViewControllerAnimated:YES]; //最上一层
[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -n)] animated:YES]; //指定哪一层,改变n