1.获取当前界面
-(UIViewController) currentViewController {
UIViewController viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
return [self atPersentViewController:viewController];
}
-(UIViewController)atPersentViewController:(UIViewController)vc {
if (vc.presentedViewController) {
return [self atPersentViewController:vc.presentedViewController];
} else if ([vc isKindOfClass:[UISplitViewController class]]) {
UISplitViewController* svc = (UISplitViewController) vc;
if (svc.viewControllers.count > 0)
return [self atPersentViewController:svc.viewControllers.lastObject];
else
return vc;
} else if ([vc isKindOfClass:[UINavigationController class]]) {
UINavigationController svc = (UINavigationController) vc;
if (svc.viewControllers.count > 0)
return [self atPersentViewController:svc.topViewController];
else
return vc;
} else if ([vc isKindOfClass:[UITabBarController class]]) {
UITabBarController svc = (UITabBarController*) vc;
if (svc.viewControllers.count > 0)
return [self atPersentViewController:svc.selectedViewController];
else
return vc;
} else {
return vc;
}
}
2.判断当前界面是否为某个界面
//获取当前界面
UIViewController *vcc = [self currentViewController];
if ([vcc isKindOfClass:[某个界面名 class]]) {
SSLog(@"vcc = %@",vcc.class);
}
网友评论