// // ShortcutManager.m // VclustersGemini // // Created by 余衡武 on 2021/10/18. // Copyright © 2021 APPLE. All rights reserved. // #import "ShortcutManager.h" #import "AppDelegate.h" static ShortcutManager *shareInstance = nil; @interface ShortcutManager () @end @implementation ShortcutManager + (instancetype)shareInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ shareInstance = [[ShortcutManager alloc] init]; }); return shareInstance; } #pragma mark 获取手机当前显示的ViewController //获取手机当前显示的ViewController 方法1-遍历法 - (UIViewController *)getCurrentViewController { UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController; while (1) { if (vc.presentedViewController) { vc = vc.presentedViewController; } else if ([vc isKindOfClass:[UITabBarController class]]) { vc = ((UITabBarController*)vc).selectedViewController; } else if ([vc isKindOfClass:[UINavigationController class]]) { vc = ((UINavigationController*)vc).visibleViewController; } else { break; } } return vc; } //获取手机当前显示的ViewController 方法1-递归法 - (UIViewController *)findCurrentShowingViewController { //获得当前活动窗口的根视图 UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController; UIViewController *currentShowingVC = [self findCurrentShowingViewControllerFrom:vc]; return currentShowingVC; } - (UIViewController *)findCurrentShowingViewControllerFrom:(UIViewController *)vc { //方法1:递归方法 Recursive method UIViewController *currentShowingVC; if ([vc presentedViewController]) { //注要优先判断vc是否有弹出其他视图,如有则当前显示的视图肯定是在那上面 // 当前视图是被presented出来的 UIViewController *nextRootVC = [vc presentedViewController]; currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC]; } else if ([vc isKindOfClass:[UITabBarController class]]) { // 根视图为UITabBarController UIViewController *nextRootVC = [(UITabBarController *)vc selectedViewController]; currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC]; } else if ([vc isKindOfClass:[UINavigationController class]]){ // 根视图为UINavigationController UIViewController *nextRootVC = [(UINavigationController *)vc visibleViewController]; currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC]; } else { // 根视图为非导航类 currentShowingVC = vc; } return currentShowingVC; } // popToRootViewController - (void)popToRootViewController:(UIViewController *)vc { // 1、popToRootViewController dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (vc.navigationController) { [vc.navigationController popToRootViewControllerAnimated:NO]; } }); // 2、tabBarController 选择第一个item (跟2不能同时执行 要做延时) vc.tabBarController.selectedIndex = 0; } // dismissToRootViewController - (void)dismissToRootViewController:(UIViewController *)vc { // 1、dismissToRootViewController while (vc.presentingViewController) { vc = vc.presentingViewController; } [vc dismissViewControllerAnimated:NO completion:^{ UIViewController *vc1 = [self findCurrentShowingViewController]; // 2、popToRootViewController dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (vc1.navigationController) { [vc1.navigationController popToRootViewControllerAnimated:NO]; } }); // 3、tabBarController 选择第一个item (跟2不能同时执行 要做延时) vc1.tabBarController.selectedIndex = 0; }]; } @end