获取当前viewcontroller

2017-05-11  本文已影响0人  凄美的月光

//获取当前屏幕显示的viewcontroller

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {

if ([rootViewController isKindOfClass:[UITabBarController class]]) {

UITabBarController* tabBarController = (UITabBarController*)rootViewController;

return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];

} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {

UINavigationController* navigationController = (UINavigationController*)rootViewController;

return [self topViewControllerWithRootViewController:navigationController.visibleViewController];

} else if (rootViewController.presentedViewController) {

UIViewController* presentedViewController = rootViewController.presentedViewController;

return [self topViewControllerWithRootViewController:presentedViewController];

}

else {

return rootViewController;

}

}

上一篇下一篇

猜你喜欢

热点阅读