微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决从 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

完美运行。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。