iOS获取View的所在的特定的parentViewContro

2021-01-28  本文已影响0人  今年27

1.当前的VC获取parentVC

+ (LKViewController*) getCurrentGroundVCWithViewController:(UIViewController*)vc{
    if (!vc) {
        return nil;
    }
    if ([vc isKindOfClass:[LKViewController class]]) {
        return vc;
    }else{
        return [self getCurrentGroundVCWithViewController:vc.parentViewController];
    }
}

2.当前View获取ParentVC

+ (LKViewController*)getCurrentGroundVCWithView:(UIView*)view{
    UIViewController* vc = [self superViewController:view];
    if (vc) {
        return [self getCurrentGroundVCWithViewController:vc];
    }
    return nil;
}

+ (UIViewController *)superViewController:(UIView*)view {
    for (UIView* next = view; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
上一篇下一篇

猜你喜欢

热点阅读