iOS页面越级跳转、指定页面跳转

iOS页面跳转方式

  1. [self presentViewController:viewController animated:YES completion:nil];
  2. [self.navigationController pushViewController:viewController animated:YES];

通常使用第2种方式,NavigationController已经帮我们管理了页面跳转形成的view controller的栈并且也提供了跳转、回退到栈内指定页面的方法:

//返回到上一个页面
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

//返回到根页面
- (NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;

//返回到栈内指定的UIViewController页面
- (NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
//返回到栈内指定的UIViewController时,栈内该UIViewController上面的所有页面都会出栈,并且参数viewController必须要保证在栈内

//跳转到指定页面常用方式1:
for (UIViewController *vc in self.navigationController.viewControllers) {
     if ([vc isKindOfClass:[yourTargetViewController class]]) {
           [self.navigationController popToViewController:vc animated:YES];
           return;
     }
 }

//跳转到指定页面常用方式2(如果知道指定页面在栈内索引位置):
UIViewController *targetVC = self.navigationController.viewControllers[1];
//UIViewController *targetVC = [self.navigationController.viewControllers objectAtIndex:1];
[self.navigationController popToViewController:targetVC animated:YES];

使用第1种presentViewController方式需要先理解:

//UIViewController内有属性
@property(nonatomic, readonly) UIViewController *presentingViewController;
@property(nonatomic, readonly) UIViewController *presentedViewController;

When you present a view controller modally (either explicitly or implicitly) using the presentViewController:animated:completion:method, the view controller that called the method has this property set to the view controller that it presented. If the current view controller did not present another view controller modally, the value in this property is nil.
简单来说就是:
[controllerA presentViewController:controllerB animated:YES completion:nil];  
controllerB.presentingViewController = controllerA;
controllerA.presentedViewController = controllerB;
controllerB.presentedViewController = nil;

理解上面的属性之后再看函数:

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion;

官方说明:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.
If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
If you want to retain a reference to the view controller'��s presented view controller, get the value in the presentedViewControllerproperty before calling this method.
The completion handler is called after the viewDidDisappear:method is called on the presented view controller.

通俗点理解就是:
页面controllerB的dismiss由展示它的页面controllerB.presentingViewController负责:

//在controllerB内执行下面的方法其实是由controllerB.presentingViewController负责处理的
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
//上述两行代码的效果是一样的,都是退出controllerB页面

如果使用present方式展示了很多个页面,形成了一个由presentedViewController形成的栈。
若在栈内某个viewControllerD(非栈顶)调用了该方法,将会dismiss它的直接child即(viewControllerD.presentedViewController)以及其上面的所有viewController。

综合上面的信息就可以实现跳转到指定viewContoller:

//直接回到根视图
UIViewController *rootVC = self.presentingViewController;
while (rootVC.presentingViewController) {
   //根视图的presentingViewController为nil退出循环
   rootVC = rootVC.presentingViewController;
}
[rootVC dismissViewControllerAnimated:YES completion:nil];

//跳转到指定的targetViewController
UIViewController *targetVC = self.presentingViewController;
 while ( ![targetVC isKindOfClass:[targetViewController class]] ) {
    targetVC = targetVC.presentingViewController;
 }
[targetVC dismissViewControllerAnimated:YES completion:nil];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,136评论 0 10
  • 文章的由来: 偶然在一次想要判断当前控制器是通过present模态推出还是通过push入栈显示的时候注意到了pre...
    东健FO_OF阅读 10,333评论 0 14
  • 每个人都有所短,有所长。我的天赋是什么呢? 我从小热爱运动,四肢修长,肌肉发达。前几天还跟18岁在学校田径队的大儿...
    李筱玲阅读 1,755评论 0 0
  • 我所说的门,不仅仅是手术室的那扇门也是生与死的那扇门。 2018.5.7 15:30 我们六个人照常在急诊...
    鹿安和阅读 3,715评论 3 3
  • “龙生龙,凤生凤,老鼠的儿子会打洞”这句俗语我初次听到时,觉得很刺耳,但是,仔细一咀嚼,就又不得不赞同国人的智慧了...
    闲散25划阅读 851评论 0 0