获取当前的 viewController
Posted 景树园
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取当前的 viewController相关的知识,希望对你有一定的参考价值。
- (UIViewController *)currentController {
UIViewController *result = nil;
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal) {
NSArray *windows = [[UIApplication sharedApplication] windows];
for (UIWindow *temWin in windows) {
if (temWin.windowLevel == UIWindowLevelNormal) {
window = temWin;
break;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nestResponder = [frontView nextResponder];
if ([nestResponder isKindOfClass:[UIViewController class]]) {
result = nestResponder;
} else {
result = window.rootViewController;
}
return result;
}
以上是关于获取当前的 viewController的主要内容,如果未能解决你的问题,请参考以下文章