iOS 如何pop到指定页面

来源:互联网 发布:java手机下载 编辑:程序博客网 时间:2024/05/21 17:34

原贴:http://blog.csdn.net/weilaizhuan/article/details/51063275

当我们做app的时候有时候会遇到这样的需求,根视图是A一个“个人信息” 页面,点击头像会push到“个人详细信息”的B页面,当我们点击某一个详细的信息进行修改时会push到“修改信息”的C页面(或者会有详细的地区选择的D、E、F等等)。修改后我们需要Pop到B视图,然而苹果给我们提供的有两种方法

1、推出到根视图控制器

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. - (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;  


2、推出到指定的视图控制器

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. - (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;  

然而当我们直接用第二种方法时系统会“崩溃”,提示

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason'Tried to pop to a view controller that doesn't exist.'  
pop推出的视图控制器并不存在,那么我们该如何使用呢?

[objc] view plain copy 在CODE上查看代码片派生到我的代码片
  1. for (UIViewController *controller in self.navigationController.viewControllers) {  
  2.             if ([controller isKindOfClass:[ReviseUserInformationViewController class]]) {  
  3.                 ReviseUserInformationViewController *revise =(ReviseUserInformationViewController *)controller;  
  4.                 [self.navigationController popToViewController:revise animated:YES];  
  5.             }  
  6.         }  

压入栈了,那我们就可以在栈内查找
0 0