码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 获取当前顶层的ViewController

时间:2017-12-21 19:40:13      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:使用   ios   div   UI   cat   pre   app   iap   color   

iOS 获取当前顶层的ViewController

#pragma mark - topVC

- (UIViewController *)theTopviewControler {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

 

使用方法

UIViewController *topmostVC = [self topViewController];

 

iOS 获取当前顶层的ViewController

标签:使用   ios   div   UI   cat   pre   app   iap   color   

原文地址:http://www.cnblogs.com/junhuawang/p/8081956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!