ios 获取当前页面所在控制器

2018-05-15  本文已影响15人  sjaljlajslkf
- (UIViewController *)currentViewController{
    
    UIViewController * currVC = nil;
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    UIViewController * Rootvc = window.rootViewController;
    
    do {
        if ([Rootvc isKindOfClass:[UINavigationController class]]) {
            UINavigationController * nav = (UINavigationController *)Rootvc;
            UIViewController * v = [nav.viewControllers lastObject];
            currVC = v;
            Rootvc = v.presentedViewController;
            continue;
        }else if([Rootvc isKindOfClass:[UITabBarController class]]){
            UITabBarController * tabVC = (UITabBarController *)Rootvc;
            currVC = tabVC;
            Rootvc = [tabVC.viewControllers objectAtIndex:tabVC.selectedIndex];
            continue;
        }
    } while (Rootvc!=nil);
    
    return currVC;
}
上一篇 下一篇

猜你喜欢

热点阅读