美文网首页
iOS多级页面强制退出

iOS多级页面强制退出

作者: 幻想无极 | 来源:发表于2020-04-07 17:49 被阅读0次
//弹出界面全部消失
+ (void)wj_AlldismissIsFinish:(void(^)(void))finish {
    UIViewController *currentVC = [self wj_findVisibleViewController];
    UIViewController *vcPresentVC = currentVC.presentingViewController;
     if (vcPresentVC) {
         while (vcPresentVC.presentingViewController)  {
             vcPresentVC = vcPresentVC.presentingViewController;
         }
         [vcPresentVC dismissViewControllerAnimated:YES completion:^{
             UIViewController *resultVC = [self wj_findVisibleViewController];
             if (resultVC.navigationController) {
                 if (resultVC.navigationController.viewControllers.count > 1) {
                    [resultVC.navigationController popToRootViewControllerAnimated:YES];
                     if (finish) {
                         finish();
                     }
                 }else {
                    if (finish) {
                        finish();
                    }
                 }
             }else {
                 if (finish) {
                     finish();
                 }
             }
         }];
     }
}

//获取根控制器
+ (UIViewController *)wj_getRootViewController {
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    return window.rootViewController;
}

//找到最顶层控制器
+ (UIViewController *)wj_findVisibleViewController {
    UIViewController* currentViewController = [self wj_getRootViewController];
    BOOL runLoopFind = YES;
    while (runLoopFind) {
            if ([currentViewController isKindOfClass:[UINavigationController class]]) {
                currentViewController = ((UINavigationController *)currentViewController).visibleViewController;
            } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
                currentViewController = ((UITabBarController* )currentViewController).selectedViewController;
            } else if ([currentViewController isKindOfClass:[UISplitViewController class]]) { // 当需要兼容 Ipad 时
                currentViewController = currentViewController.presentedViewController;
            } else {
                if (currentViewController.presentedViewController) {
                    currentViewController = currentViewController.presentedViewController;
                     NSLog(@"%@",[currentViewController class]);
                } else {
                    return currentViewController;
                }
            }
    }
    return currentViewController;
}

相关文章

网友评论

      本文标题:iOS多级页面强制退出

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