union_ad_ssgf/ios/Classes/utils/TUIViewController+getCurrentVC.m
2024-11-26 11:14:20 +08:00

90 lines
3.1 KiB
Objective-C

#import "TUIViewController+getCurrentVC.h"
@implementation UIViewController (getCurrentVC)
+ (UIViewController *)jsd_getRootViewController{
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
//NSAssert(window, @"The window is empty");
return window.rootViewController;
}
+ (UIViewController *)jsd_getCurrentViewController{
UIViewController* currentViewController = [self jsd_getRootViewController];
BOOL runLoopFind = YES;
while (runLoopFind) {
if (currentViewController.presentedViewController) {
currentViewController = currentViewController.presentedViewController;
} else if ([currentViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController* )currentViewController;
currentViewController = [navigationController.childViewControllers lastObject];
} else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController* )currentViewController;
currentViewController = tabBarController.selectedViewController;
} else {
NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
if (childViewControllerCount > 0) {
currentViewController = currentViewController.childViewControllers.lastObject;
return currentViewController;
} else {
return currentViewController;
}
}
}
return currentViewController;
}
+ (UIViewController *)getCurrentVCWithCurrentView:(UIView *)currentView
{
for (UIView *next = currentView ; next ; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
+(UITabBarController *)currentTtabarController
{
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
UIViewController *tabbarController = window.rootViewController;
if ([tabbarController isKindOfClass:[UITabBarController class]]) {
return (UITabBarController *)tabbarController;
}
return nil;
}
+(UINavigationController *)currentTabbarSelectedNavigationController
{
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
UIViewController *rootVC = window.rootViewController;
if ([rootVC isKindOfClass:[UINavigationController class]]) {
return (UINavigationController *)rootVC;
}else if([rootVC isKindOfClass:[UITabBarController class]]){
UITabBarController *tabarController = [self currentTtabarController];
UINavigationController *selectedNV = (UINavigationController *)tabarController.selectedViewController;
if ([selectedNV isKindOfClass:[UINavigationController class]]) {
return selectedNV;
}
}
return nil;
}
@end