在开发过程中,当我们在View里面处理一些事件的时候,可能会用到当前viewController、NavigationController,或者tabBarController去处理一些事件,实现当然,方法有很多种,比如通知代理block这些应该都可以实现,但是今天我向大家分享的几种方法就是通过Window直接拿到你想要的控制器。
1.获取当前所在的TabBarController
+(UITabBarController *)currentTtabarController
{
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
UIViewController *tabbarController = window.rootViewController;
if ([tabbarController isKindOfClass:[UITabBarController class]]) {
return (UITabBarController *)tabbarController;
}
return nil;
}
2.获取当前TabBarController所选中的navigationController
+(UINavigationController *)currentTabbarSelectedNavigationController
{
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
UIViewController *rootVC = window.rootViewController;
if ([rootVC isKindOfClass:[UINavigationController class]]) {
return (UINavigationController *)rootVC;
}else if([rootVC isKindOfClass:[UITabBarController class]]){
UITabBarController *tabarController = [self currentTtabarController];
UINavigationController *selectedNV = (UINavigationController *)tabarController.selectedViewController;
if ([selectedNV isKindOfClass:[UINavigationController class]]) {
return selectedNV;
}
}
return nil;
}
3.获取当前控制器
- 方法一
+(UIViewController*)getCurrentViewController{
UINavigationController *selectedNV = [self currentTabbarSelectedNavigationController];
if (selectedNV.viewControllers.count > 0) {
return [selectedNV.viewControllers lastObject];
}
return nil;
}
-方法二
+ (UIViewController *)cpx_getCurrentDisplayController {
__block UIWindow *normalWindow = [[UIApplication sharedApplication] keyWindow];
NSArray *windows = [[UIApplication sharedApplication] windows];
if (normalWindow.windowLevel != UIWindowLevelNormal) {
[windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.windowLevel == UIWindowLevelNormal) {
normalWindow = obj;
*stop = YES;
}
}];
}
return [self cpx_getTopViewController:normalWindow.rootViewController];
}
+ (UIViewController *)cpx_getTopViewController:(UIViewController *)inViewController {
while (inViewController.presentedViewController) {
inViewController = inViewController.presentedViewController;
}
if ([inViewController isKindOfClass:[UITabBarController class]]) {
UIViewController *selectedVC = [self cpx_getTopViewController:((UITabBarController *)inViewController).selectedViewController];
return selectedVC;
} else if ([inViewController isKindOfClass:[UINavigationController class]]) {
UIViewController *selectedVC = [self cpx_getTopViewController:((UINavigationController *)inViewController).visibleViewController];
return selectedVC;
} else {
return inViewController;
}
}
4.通过View获取View的父控制器
+ (UIViewController *)cpx_getParentControllerFromView:(UIView *)view
{
UIResponder *responder = [view nextResponder];
while (responder)
{
if ([responder isKindOfClass:[UIViewController class]])
{
return (UIViewController *)responder;
}
responder = [responder nextResponder];
}
return nil;
}
5.通过递归方法遍历当前View的所有子试图
+(void)getMysubViewsWithViews:(UIView *)view{
NSArray *arrayViews = view.subviews;
for (UIView * obj in arrayViews) {
if ([obj isKindOfClass:[MyView class]]) {
NSLog(@"找到了 %@",MyView);
}
[self getMysubViewsWithViews:obj];
}
}
网友评论