iOS获取当前的ViewController

2017-08-30  本文已影响0人  清蘂翅膀的技术

+(UIViewController *)getCurrentVC

{

UIViewController *result = nil;

UIWindow * window = [[UIApplication sharedApplication] keyWindow];

//app默认windowLevel是UIWindowLevelNormal,如果不是,找到UIWindowLevelNormal的

if (window.windowLevel != UIWindowLevelNormal)

{

NSArray *windows = [[UIApplication sharedApplication] windows];

for(UIWindow * tmpWin in windows)

{

if (tmpWin.windowLevel == UIWindowLevelNormal)

{

window = tmpWin;

break;

}

}

}

id  nextResponder = nil;

UIViewController *appRootVC=window.rootViewController;

//    如果是present上来的appRootVC.presentedViewController 不为nil

if (appRootVC.presentedViewController) {

nextResponder = appRootVC.presentedViewController;

}else{

NSLog(@"===%@",[window subviews]);

UIView *frontView = [[window subviews] objectAtIndex:0];

nextResponder = [frontView nextResponder];

}

if ([nextResponder isKindOfClass:[UITabBarController class]]){

UITabBarController * tabbar = (UITabBarController *)nextResponder;

UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];

//        UINavigationController * nav = tabbar.selectedViewController ; 上下两种写法都行

result=nav.childViewControllers.lastObject;

}else if ([nextResponder isKindOfClass:[UINavigationController class]]){

UIViewController * nav = (UIViewController *)nextResponder;

result = nav.childViewControllers.lastObject;

}else{

result = nextResponder;

}

return result;

}

上一篇下一篇

猜你喜欢

热点阅读