+ (UIViewController *)currentViewController
{
UIViewController *frontUIViewController = nil;
UIWindow *defaultWindow = [[UIApplication sharedApplication] keyWindow];
if (defaultWindow.windowLevel != UIWindowLevelNormal) {
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *tmpWin in windows) {
if ([tmpWin isKindOfClass:[UIWindow class]] && tmpWin.windowLevel == UIWindowLevelNormal) {
defaultWindow = tmpWin;
break;
}
}
}
if (defaultWindow && [[defaultWindow subviews] count] > 0) {
UIView *frontView = [[defaultWindow subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
frontUIViewController = nextResponder;
} else {
frontUIViewController = defaultWindow.rootViewController;
}
if (frontUIViewController) {
frontUIViewController = [self topDisplayViewController:frontUIViewController];
while (frontUIViewController && frontUIViewController.presentedViewController) {
frontUIViewController = [self topDisplayViewController:frontUIViewController.presentedViewController];
}
}
}
return frontUIViewController;
}
+ (UIViewController *)topDisplayViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
return [self topDisplayViewController:[(UINavigationController *)viewController topViewController]];
} else if ([viewController isKindOfClass:[UITabBarController class]]) {
return [self topDisplayViewController:[(UITabBarController *)viewController selectedViewController]];
} else {
return viewController;
}
return nil;
}
iOS 当前控制器
猜你喜欢
转载自blog.csdn.net/ivolcano/article/details/88603096
今日推荐
周排行