swift 获取控制器

版权声明:转载请标明出处 https://blog.csdn.net/ZY_FlyWay/article/details/81059774

得到view的控制器


/**
    view 获取它的控制器
    */
    public func viewController()->UIViewController? {

        var nextResponder: UIResponder? = self

        repeat {
            nextResponder = nextResponder?.next

            if let viewController = nextResponder as? UIViewController {
                return viewController
            }

        } while nextResponder != nil

        return nil
    }

得到当前控制器
用途:可以判断当前控制器是哪个控制器 等


///获取当前控制器
func currentVc() ->UIViewController{

    var vc = UIApplication.shared.keyWindow?.rootViewController

    if (vc?.isKind(of: UITabBarController.self))! {
        vc = (vc as! UITabBarController).selectedViewController
    }else if (vc?.isKind(of: UINavigationController.self))!{
        vc = (vc as! UINavigationController).visibleViewController
    }else if ((vc?.presentedViewController) != nil){
        vc =  vc?.presentedViewController
    }

    return vc!

} 
阅读更多

没有更多推荐了,返回首页