• 获取最顶层的视图控制器 iOS


    1、获取当前所展示的控制器:

    //需要获取到显示在最上面的viewController
    - (UIViewController *)theTopviewControler{
        //获取根控制器
        UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
        
        UIViewController *parent = rootVC;
        //遍历 如果是presentViewController
        while ((parent = rootVC.presentedViewController) != nil ) {
            rootVC = parent;
        }
       
        while ([rootVC isKindOfClass:[UINavigationController class]]) {
            rootVC = [(UINavigationController *)rootVC topViewController];
        }
        return rootVC;
    }

    2.找到当前视图的父视图控制器

    - (UIViewController *)viewController {
    
        for(UIView* next = [self superview]; next; next = next.superview) {
    
            UIResponder*nextResponder = [next nextResponder];
    
            if ([nextResponder isKindOfClass:[UIViewController class]]) {
    
                return(UIViewController*)nextResponder;
    
            }
        }
    
        return nil;
    }
  • 相关阅读:
    WordCount结对项目
    第一周作业:一些感想
    第一次作业
    Spring Cloud 微服务实战笔记
    解决jest处理es模块
    领域驱动设计(DDD:Domain-Driven Design)
    测试
    whistle
    日记(2018-11-07)
    ubuntu中使用机密数据Secrets
  • 原文地址:https://www.cnblogs.com/huangzs/p/16162796.html
Copyright © 2020-2023  润新知