谁的观点不在windows heirarchy

时间:2017-07-03 16:07:42

标签: mvvm swift3 xcode8 ios10

我知道之前已经问过这个问题,但其他问题的答案都没有对我有用。这是我的代码:

```

.yourapp

```

这是我的错误: var values = [String: AnyObject]() func loginUserToFirebase(_ completion: () -> Void) { let accessToken = FBSDKAccessToken.current() guard let accessTokenString = accessToken?.tokenString else {fatalError()} let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString) FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in if error != nil { print(error ?? "Something went wrong") return } self.fbGraphRequest() }) } internal func fbGraphRequest(){ FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start { (connection, result, error) in if error != nil { print(error ?? "error unknown") return } else { print(result ?? "no result") self.values = result as! [String: AnyObject] print(self.values) weak var rootViewModel = RootViewModel() rootViewModel?.values = self.values self.presentRootViewController() } } } internal func presentRootViewController() { let loginController = LoginController() let rootViewController = RootViewController() loginController.present(rootViewController, animated: true, completion: nil) } 当我在LoginViewController中使用此代码片段并使用 Attempt to present <Art_Cache.RootViewController: 0x7fa6a1c2aab0> on <Art_Cache.LoginController: 0x7fa6a1c840b0> whose view is not in the window hierarchy! 时,此代码段有效。我试图将我的项目转换为MVVM,这就是正在发生的事情。问题似乎在self.present(rootViewController, animation: true, completion: nil)附近。按下facebook登录按钮时会触发这些功能。请帮助和欢呼!

1 个答案:

答案 0 :(得分:1)

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

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)