美文网首页
iOS页面越级跳转、指定页面跳转

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

作者: Joker_Lee | 来源:发表于2018-10-24 19:14 被阅读0次

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];

相关文章

网友评论

      本文标题:iOS页面越级跳转、指定页面跳转

      本文链接:https://www.haomeiwen.com/subject/sesxtqtx.html