Skip to content

Instantly share code, notes, and snippets.

@yunyang088
Created June 1, 2016 09:19
Show Gist options
  • Save yunyang088/3d09865da73747df415ad3b74eb90066 to your computer and use it in GitHub Desktop.
Save yunyang088/3d09865da73747df415ad3b74eb90066 to your computer and use it in GitHub Desktop.
//获取当前屏幕显示的viewcontroller
- (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
result = window.rootViewController;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment