swift 获取当前viewcontroller (rootVC)

xcode 报错
whose view is not in the window hierarchy!

也就是你现在的Present的视图并不是Windows视图

///获取当前控制器
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!

}

你可能感兴趣的:(swift,获取主vc,rootvc)