iOS开发

iOS开发之获取当前栈中的控制器

2019-01-17  本文已影响0人  悄然林静
/**
 查找栈中的当前控制器

 @return 控制器
 */
- (UIViewController *)findViewControllerInStack {
    UIViewController *currVC = nil;
    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.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;
}
上一篇下一篇

猜你喜欢

热点阅读