gpt4 book ai didi

mvvm - 其 View 不在窗口层次结构中

转载 作者:行者123 更新时间:2023-12-03 10:33:27 25 4
gpt4 key购买 nike

我知道之前曾有人问过这个问题,但其他问题中没有一个答案对我有用。这是我的代码:

```

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)

}

```

这是我的错误:
Attempt to present <Art_Cache.RootViewController: 0x7fa6a1c2aab0> on <Art_Cache.LoginController: 0x7fa6a1c840b0> whose view is not in the window hierarchy!
当我在LoginViewController中使用此代码片段并使用 self.present(rootViewController, animation: true, completion: nil)时,此代码片段有效。我试图将我的项目转换为MVVM,这就是正在发生的事情。问题似乎出在 self.presentRootViewController()周围。按下facebook登录按钮时将触发这些功能。请帮助和欢呼!

最佳答案

当您当前的 View Controller 不在窗口中时,会发生这种情况吗?

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)

关于mvvm - 其 View 不在窗口层次结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44889983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com