-2

所以我的目标是能够显示正确的 vc 而没有任何错误。这个问题实际上有多个错误。首先,显示代码块并首先解释它是有意义的,这样您就可以了解我正在谈论的内容。

在我的SceneDelegate.swift willConnectTo()方法中,我有这段代码......

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    self.window = self.window ?? UIWindow()

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let auth = Auth.auth()
    let actualuser = auth.currentUser
    


    auth.addStateDidChangeListener { (_, user) in
        
        if let user = user {
            
            db.document("student_users/\(actualuser?.uid)").getDocument { (docSnapshot, error) in
                if error != nil {
                    print("\(error)")
                } else {
                    guard let docSnap = docSnapshot?.exists else { return }
                    if docSnap == true {
                        let alreadyLoggedInAsAStudentViewController = storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.StudentEventDashboardStoryboardID) as! StudentSegmentedTableViewController
                        let navigationizedVC = UINavigationController(rootViewController: alreadyLoggedInAsAStudentViewController)
                        self.window!.rootViewController = navigationizedVC
                        self.window!.makeKeyAndVisible()
                    } else {
                        let alreadyLoggedInAsASchoolViewController = storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.SchoolEventDashboardStoryboardID) as! SchoolTableViewController
                        let navigationizedSchoolVC = UINavigationController(rootViewController: alreadyLoggedInAsASchoolViewController)
                        self.window!.rootViewController = navigationizedSchoolVC
                        self.window!.makeKeyAndVisible()
                    }
                }
            }
            
        }
        
    }
    
    guard let _ = (scene as? UIWindowScene) else { return }
}

所以在我的应用程序中,我有两种类型的用户,并且我使用了一些逻辑来实例化正确的 viewController。问题是这样的,假设我以学生身份登录,最小化模拟器窗口,然后重新运行模拟器,它显示错误的 vc,但是当我再次执行相同操作时,它显示正确的 vc。同样的事情也适用于其他用户。还有另一个错误,说用户已注销并且状态侦听器未激活,但是一旦我以学校用户身份登录,vcs 就会再次切换,但如果我重新运行模拟器,它显然会显示正确的 vc .

我在控制台中也遇到了这个错误,我查了一下,但我无法将我在其他问题中看到的内容应用到我的问题中。

错误

这是问题的 GIF。当我以学校用户身份登录时,您可以看到 vc 是如何出错的,但是当我最小化模拟器窗口并重新运行它时,会显示正确的 vc。只是想知道如何防止这种情况。谢谢

gif

4

1 回答 1

0

你需要像这样启动你的窗口:

guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
self.window = window

仅在使用不带 sceneDelegate 的 appDelegate 时才建议实例化 UIWindow,这样可能会导致内存问题

另外,请记住,您在打开应用程序之前调用的是文档。因此,请检查变量docSnap是否第一次没有返回 false。也许就是这样。但是在您的代码中并不清楚哪个视图是哪个

于 2021-02-28T08:15:44.373 回答