引用worldzhy 的文章
worldzhy大神的 方法是分开的,刚好我们项目里面用到了这个,两种弹出VC方式都遇到了,所以就把两个方法合并为一个,这样用起来更方便。
//获取当前屏幕显示的viewcontroller
+ (UIViewController *)findCurrentResponderViewController
{
UIViewController *currentVC = 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]])
currentVC = nextResponder;
else{
UIViewController *topVC = window.rootViewController.presentedViewController;
if (topVC) {
currentVC = topVC;
}else{
currentVC = window.rootViewController;
}
}
return currentVC;
}