从 SceneDelegate 实例化时如何获取完整的 viewController

问题描述

所以在我的最后一个问题中,我试图从 AppDelegate 实例化,但它不起作用,所以在做了更多的研究之后,我一直看到 SceneDelegate 并决定试一试。令我惊讶的是,它奏效了,但并不是我想要的。

所以这是登录时 viewController 的正常图像。

normal VC

不要介意空格,我在自动布局方面遇到了问题,我将来会解决这个问题。我希望在从 SceneDelegate.swift 实例化 VC 时显示准确的图像,其中包含按钮、导航栏、用户交互以及正确显示的所有内容。

现在,当我在 SceneDelegate 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()

    
    auth.addStateDidChangeListener { (_,user) in

        if let user = user {
            let alreadyLoggedInViewController = storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.StudentEventDashboardStoryboardID) as! StudentSegmentedTableViewController
            self.window!.rootViewController = alreadyLoggedInViewController
            self.window!.makeKeyAndVisible()
        }



    }
    
    guard let _ = (scene as? UIWindowScene) else { return }
}

我最终得到了这个......

messedup VC

我真的很高兴看到它,因为我的代码终于可以工作了,但它没有显示带有栏按钮项的导航栏,这有点压抑了快乐。分段控件仍然是交互式的,tableview 仍然会刷新,这很好,但我只想要我的普通 VC,这样用户就可以正常使用该应用程序并查看所有其他 UI。如果有人可以帮助解决这个问题,那对我来说将是一个很大的帮助。提前致谢。

解决方法

太棒了,我需要做的就是添加这行代码..

 let navigationizedVC = UINavigationController(rootViewController: alreadyLoggedInViewController)

然后就这样做...

             self.window!.rootViewController = navigationizedVC

完美运行。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...