chenzhao

  • java
  • iOS
  • IT
知识积累
不积跬步无以至千里
  1. 首页
  2. iOS
  3. 正文

寻找上层的ViewController

2016年 8月 30日 77点热度 0人点赞 0条评论
extension UIView {
    

    func findController() -> UIViewController! {
        return self.findControllerWithClass(UIViewController.self)
    }
    
    func findNavigator() -> UINavigationController! {
        return self.findControllerWithClass(UINavigationController.self)
    }
    
    func findControllerWithClass<T>(clzz: AnyClass) -> T? {
        var responder = self.nextResponder()
        while(responder != nil) {
            if (responder!.isKindOfClass(clzz)) {
                return responder as? T
            }
            responder = responder?.nextResponder()
        }
        
        return nil
    }
    
}

还有一种通过rootviewController 寻找的

//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    
    return currentVC;
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        
        currentVC = rootVC;
    }
    
    return currentVC;
}

//swift

       /// 获取当前显示的VC
    func getCurrentVC()->UIViewController{
        let rootViewController = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController
        //        if rootViewController == nil{return nil}
        let currentVC = self.getCurrentVCFrom(rootVC: rootViewController!)
        return currentVC
    }
    fileprivate func getCurrentVCFrom(rootVC:UIViewController)->UIViewController{
        //var currentVC:UIViewController!;
        var currentVC:UIViewController = rootVC; // 这里先赋值处理,不然会崩溃(特定条件下)
        var rootVC2 = rootVC
        if rootVC.presentedViewController != nil{
            rootVC2 = rootVC2.presentedViewController!
        }
        if (rootVC2.isKind(of: UITabBarController.classForCoder())){
            rootVC2 = self.getCurrentVCFrom(rootVC: (rootVC2 as! UITabBarController).selectedViewController!)
        }else if rootVC2.isKind(of: UINavigationController.classForCoder()){
            currentVC = self.getCurrentVCFrom(rootVC: (rootVC2 as! UINavigationController).visibleViewController!)
        }else{
            currentVC = rootVC2
        }
        return currentVC
    }
    
标签: 暂无
最后更新:2022年 11月 11日

陈昭

IT 程序员

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

COPYRIGHT © 2022 chenzhao. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang