Mvvm 谁的视图不在windows继承人制度中

Mvvm 谁的视图不在windows继承人制度中,mvvm,swift3,xcode8,ios10,Mvvm,Swift3,Xcode8,Ios10,我知道以前有人问过这个问题,但其他问题的答案都不适合我。这是我的密码: ``` ``` 这是我的错误: 尝试显示不在窗口层次结构中的视图! 当我在LoginViewController中使用self.present(rootViewController,animation:true,completion:nil)时,这个代码片段就起作用了。我正在尝试将我的项目转换为MVVM,这就是正在发生的事情。问题似乎出在self.presentRootViewController()上。这些功能在按下f

我知道以前有人问过这个问题,但其他问题的答案都不适合我。这是我的密码:

```

```

这是我的错误:

尝试显示不在窗口层次结构中的视图!

当我在LoginViewController中使用self.present(rootViewController,animation:true,completion:nil)时,这个代码片段就起作用了。我正在尝试将我的项目转换为MVVM,这就是正在发生的事情。问题似乎出在self.presentRootViewController()上。这些功能在按下facebook登录按钮时启动。请帮忙,干杯

如果当前视图控制器不是窗口的一部分,则会发生这种情况,是否可以更改

func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {

        if let nav = base as? UINavigationController {
            return topViewController(nav.visibleViewController)
        }

        if let tab = base as? UITabBarController {
            let moreNavigationController = tab.moreNavigationController

            if let top = moreNavigationController.topViewController, top.view.window != nil {
                return topViewController(top)
            } else if let selected = tab.selectedViewController {
                return topViewController(selected)
            }
        }

        if let presented = base?.presentedViewController {
            return topViewController(presented)
        }

        return base
    }


    topViewController()?.present(rootViewController, animated: true, 
    completion: nil)

谢谢你,先生。你是摇滚明星!我尝试过此代码的一个变体,但它不起作用,因此我认为问题不在于
UIApplication.shared.keyWindow.rootViewcontroller
。再次感谢你!您甚至包括导航和选项卡选择。你真是个摇滚明星!
func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {

        if let nav = base as? UINavigationController {
            return topViewController(nav.visibleViewController)
        }

        if let tab = base as? UITabBarController {
            let moreNavigationController = tab.moreNavigationController

            if let top = moreNavigationController.topViewController, top.view.window != nil {
                return topViewController(top)
            } else if let selected = tab.selectedViewController {
                return topViewController(selected)
            }
        }

        if let presented = base?.presentedViewController {
            return topViewController(presented)
        }

        return base
    }


    topViewController()?.present(rootViewController, animated: true, 
    completion: nil)