//获取当前的control
-
(UIViewController *)getCurrentViewController {
UIViewController *result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
NSLog(@"window:%@ window.windowLevel:%f",window,window.windowLevel);
//app默认windowLevel是UIWindowLevelNormal,如果不是,找到它
if (window.windowLevel != UIWindowLevelNormal) {
NSLog(@"window:%@ window.windowLevel:%f",window,window.windowLevel);
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows) {
if (tmpWin.windowLevel == UIWindowLevelNormal) {
NSLog(@"tmpWin:%@ tmpWin.windowLevel:%f",tmpWin,tmpWin.windowLevel);
window = tmpWin;
break;
}
}
}
id nextResponder = nil;
UIViewController *appRootVC = window.rootViewController;
//1、通过present弹出VC,appRootVC.presentedViewController不为nil
if (appRootVC.presentedViewController) {
NSLog(@"subviews = %@",[window subviews]);
nextResponder = appRootVC.presentedViewController;
}else{
//2、通过navigationcontroller弹出VC
NSLog(@"subviews == %@",[window subviews]);
UIView *frontView = [[window subviews] objectAtIndex:0];
nextResponder = [frontView nextResponder];
}
//1、tabBarController
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]]){
//2、navigationControllerUIViewController * nav = (UIViewController *)nextResponder; result = nav.childViewControllers.lastObject;
}else{//3、viewControler
result = nextResponder;
}
return result;
}
网友评论