获取最上层的ViewController-IOS

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ws_752958369/article/details/82858059
- (UIViewController *)topViewController{
    UIViewController *vc = UIApplication.sharedApplication.keyWindow.rootViewController;
    while (  [vc isKindOfClass:[UINavigationController class]] || [vc isKindOfClass:[UITabBarController class]] ) {
        if ( [vc isKindOfClass:[UINavigationController class]] ) vc = [(UINavigationController *)vc topViewController];
        if ( [vc isKindOfClass:[UITabBarController class]] ) vc = [(UITabBarController *)vc selectedViewController];
        if ( vc.presentedViewController ) vc = vc.presentedViewController;
    }
    return vc;
}

猜你喜欢

转载自blog.csdn.net/ws_752958369/article/details/82858059
今日推荐