//获取到nav 级别的
- (UIViewController *)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
if ([rootViewController isKindOfClass:[CustomTabbarController class]]) {
CustomTabbarController *tabBarController = (CustomTabbarController *)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
}
else
{
return rootViewController;
}
}
//获取当前显示的viewcontroller
- (UIViewController *)ViewControllerWithRootViewController:(UIViewController*)rootViewController
{
if ([rootViewController isKindOfClass:[CustomTabbarController class]]) {
CustomTabbarController *tabBarController = (CustomTabbarController *)rootViewController;
return [self ViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self ViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self ViewControllerWithRootViewController:presentedViewController];
}
else
{
return rootViewController;
}
}
//获取当前页面的controller
- (UIViewController *)getCurrentVC
{
UIViewController *result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
result = nextResponder;
else if ([window respondsToSelector:@selector(rootViewController)] && window.rootViewController != nil)
result = window.rootViewController;
return result;
}