记录一下自己遇到的navigationController比较特殊的返回情况
navigationController有一个自己的存放所有push的VC的数组,navigationController.viewControllers,可以通过这个数组灵活控制跳转
1、返回指定界面
for (UIViewController * controller in self.navigationController.viewControllers) { //遍历navigationController中所有的VC
if ([controller isKindOfClass:[YourViewController class]]) { //这里判断是否为你想要跳转的页面
[weakself.navigationController popToViewController:controller animated:YES];
break;
}
}
2、返回两个界面或以上
int index = (int)[[self.navigationController viewControllers]indexOfObject:self];
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:(index - n)] animated:YES];//n为跳转回第几个界面,上一个界面是1,再上一个界面是2,以此类推
3、返回上个界面并传参
//此页面已经存在于self.navigationController.viewControllers中,并且是当前页面的前一页面
YourViewController *vc = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
vc.yourObject = object;
//使用popToViewController返回并传值到上一页面
[self.navigationController popToViewController:vc animated:YES];
网友评论