SwiftUI go back to RootView in sceneWillEnterForeground SceneDelegate.swift

后端 未结 1 1415
时光说笑
时光说笑 2021-01-23 00:56

we try to reset the current View to Root if App will enter Foreground.

How can we do that in SwiftUI?

func sceneWillEnterForeground(_ scene: UIScene) {
          


        
1条回答
  •  时光说笑
    2021-01-23 01:44

    There is no "go back" but the possible approach is to recreate root view controller, by moving "by default" generated content creation into other delegate method as below...

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            self.window = window
        }
    }
    
    
    func sceneWillEnterForeground(_ scene: UIScene) {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        let contentView = RootView().environment(\.managedObjectContext, context)
        let singleOrder = SingleOrder()
        window?.rootViewController = UIHostingController(rootView: contentView.environmentObject(singleOrder))
        window?.makeKeyAndVisible()
    }
    

    0 讨论(0)
提交回复
热议问题