获取当前最顶层的VC

时间:2023-03-08 17:29:02

#pragma mark -  获取当前最顶层的ViewController

- (UIViewController*)topVC:(UIViewController*)VC {

if([VC isKindOfClass:[UINavigationControllerclass]]) {

return[selftopVC:[(UINavigationController*)VC topViewController]];

}

if([VC isKindOfClass:[UITabBarControllerclass]]) {

return[selftopVC:[(UITabBarController*)VC selectedViewController]];

}

returnVC;

}

- (UIViewController*)topViewController {

UIViewController*vc = [selftopVC:[UIApplicationsharedApplication].keyWindow.rootViewController];

while(vc.presentedViewController) {

vc = [selftopVC:vc];

}

returnvc;

}