- //Get the ViewController currently displayed by Window
- - (UIViewController*)currentViewController{
- //Get the root view of the currently active window
- UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
- while (1)
- {
- //According to different page switching methods, gradually obtain the top viewController
- if ([vc isKindOfClass:[UITabBarController class]]) {
- vc = ((UITabBarController*)vc).selectedViewController;
- }
- if ([vc isKindOfClass:[UINavigationController class]]) {
- vc = ((UINavigationController*)vc).visibleViewController;
- }
- if (vc.presentedViewController) {
- vc = vc.presentedViewController;
- }else{
- break;
- }
- }
- return vc;
}