iOS 如何pop到指定页面

来源:互联网 发布:h3c 端口镜像 编辑:程序博客网 时间:2024/05/21 23:51

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

1、推出到根视图控制器

- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;


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

- (nullable NSArray<__kindof UIViewController *> *)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 *controller in self.navigationController.viewControllers) {            if ([controller isKindOfClass:[ReviseUserInformationViewController class]]) {                ReviseUserInformationViewController *revise =(ReviseUserInformationViewController *)controller;                [self.navigationController popToViewController:revise animated:YES];            }        }

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

2 0