ShortcutManager.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // ShortcutManager.m
  3. // VclustersGemini
  4. //
  5. // Created by 余衡武 on 2021/10/18.
  6. // Copyright © 2021 APPLE. All rights reserved.
  7. //
  8. #import "ShortcutManager.h"
  9. #import "UseAccountManage.h"
  10. #import "AppDelegate.h"
  11. static ShortcutManager *shareInstance = nil;
  12. @interface ShortcutManager ()
  13. @end
  14. @implementation ShortcutManager
  15. + (instancetype)shareInstance {
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. shareInstance = [[ShortcutManager alloc] init];
  19. });
  20. return shareInstance;
  21. }
  22. #pragma mark 获取手机当前显示的ViewController
  23. //获取手机当前显示的ViewController 方法1-遍历法
  24. - (UIViewController *)getCurrentViewController {
  25. UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
  26. while (1)
  27. {
  28. if (vc.presentedViewController) {
  29. vc = vc.presentedViewController;
  30. } else if ([vc isKindOfClass:[UITabBarController class]]) {
  31. vc = ((UITabBarController*)vc).selectedViewController;
  32. } else if ([vc isKindOfClass:[UINavigationController class]]) {
  33. vc = ((UINavigationController*)vc).visibleViewController;
  34. } else {
  35. break;
  36. }
  37. }
  38. return vc;
  39. }
  40. //获取手机当前显示的ViewController 方法1-递归法
  41. - (UIViewController *)findCurrentShowingViewController {
  42. //获得当前活动窗口的根视图
  43. UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
  44. UIViewController *currentShowingVC = [self findCurrentShowingViewControllerFrom:vc];
  45. return currentShowingVC;
  46. }
  47. - (UIViewController *)findCurrentShowingViewControllerFrom:(UIViewController *)vc
  48. {
  49. //方法1:递归方法 Recursive method
  50. UIViewController *currentShowingVC;
  51. if ([vc presentedViewController]) { //注要优先判断vc是否有弹出其他视图,如有则当前显示的视图肯定是在那上面
  52. // 当前视图是被presented出来的
  53. UIViewController *nextRootVC = [vc presentedViewController];
  54. currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
  55. } else if ([vc isKindOfClass:[UITabBarController class]]) {
  56. // 根视图为UITabBarController
  57. UIViewController *nextRootVC = [(UITabBarController *)vc selectedViewController];
  58. currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
  59. } else if ([vc isKindOfClass:[UINavigationController class]]){
  60. // 根视图为UINavigationController
  61. UIViewController *nextRootVC = [(UINavigationController *)vc visibleViewController];
  62. currentShowingVC = [self findCurrentShowingViewControllerFrom:nextRootVC];
  63. } else {
  64. // 根视图为非导航类
  65. currentShowingVC = vc;
  66. }
  67. return currentShowingVC;
  68. }
  69. // popToRootViewController
  70. - (void)popToRootViewController:(UIViewController *)vc {
  71. // 1、popToRootViewController
  72. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  73. if (vc.navigationController) {
  74. [vc.navigationController popToRootViewControllerAnimated:NO];
  75. }
  76. });
  77. // 2、tabBarController 选择第一个item (跟2不能同时执行 要做延时)
  78. vc.tabBarController.selectedIndex = 0;
  79. }
  80. // dismissToRootViewController
  81. - (void)dismissToRootViewController:(UIViewController *)vc {
  82. // 1、dismissToRootViewController
  83. while (vc.presentingViewController) {
  84. vc = vc.presentingViewController;
  85. }
  86. [vc dismissViewControllerAnimated:NO completion:^{
  87. UIViewController *vc1 = [self findCurrentShowingViewController];
  88. // 2、popToRootViewController
  89. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  90. if (vc1.navigationController) {
  91. [vc1.navigationController popToRootViewControllerAnimated:NO];
  92. }
  93. });
  94. // 3、tabBarController 选择第一个item (跟2不能同时执行 要做延时)
  95. vc1.tabBarController.selectedIndex = 0;
  96. }];
  97. }
  98. @end