-(UIViewController *)getCurrentVC
{
UIViewController*result =nil;
UIWindow* window = [[UIApplicationsharedApplication]keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray*windows = [[UIApplicationsharedApplication]windows];
for(UIWindow* tmpWininwindows)
{
if(tmpWin.windowLevel==UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
//从根控制器开始查找
UIViewController*rootVC = window.rootViewController;
id nextResponder= [rootVC.viewnextResponder];
if([nextResponderisKindOfClass:[UINavigationControllerclass]]) {
result = ((UINavigationController*)nextResponder).topViewController;
if([resultisKindOfClass:[UITabBarControllerclass]]) {
result = ((UITabBarController*)result).selectedViewController;
}
}else if([nextResponderisKindOfClass:[UITabBarControllerclass]]) {
result = ((UITabBarController*)nextResponder).selectedViewController;
if([resultisKindOfClass:[UINavigationControllerclass]]) {
result = ((UINavigationController*)result).topViewController;
}
}else if([nextResponderisKindOfClass:[UIViewControllerclass]]) {
result = nextResponder;
}else{
result = window.rootViewController;
if([resultisKindOfClass:[UINavigationControllerclass]]) {
result = ((UINavigationController*)result).topViewController;
if([resultisKindOfClass:[UITabBarControllerclass]]) {
result = ((UITabBarController*)result).selectedViewController;
}
}else if([resultisKindOfClass:[UIViewControllerclass]]) {
result = nextResponder;
}
}
returnresult;
}
网友评论