swift|swift 获取当前viewcontroller (rootVC)

【swift|swift 获取当前viewcontroller (rootVC)】xcode 报错
whose view is not in the window hierarchy!
也就是你现在的Present的视图并不是Windows视图
///获取当前控制器
func currentVc() ->UIViewController{

var vc = UIApplication.shared.keyWindow?.rootViewControllerif (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!

}

    推荐阅读