1、获取当前控制器
//获取手机当前显示的ViewController
+ (UIViewController*)currentViewController{
UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
while (1) {
if ([vc isKindOfClass:[UITabBarController class]]) {
vc = ((UITabBarController*)vc).selectedViewController;
}
if ([vc isKindOfClass:[UINavigationController class]]) {
vc = ((UINavigationController*)vc).visibleViewController;
}
if (vc.presentedViewController) {
vc = vc.presentedViewController;
}else{
break;
}
}
return vc;
2.获取顶层控制器
- (UIViewController *)appRootViewController {
UIViewController *RootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *topVC = RootVC;
while (topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
return topVC;
}
网友评论