SwiftUI: How could I show the new View base on the current Scene

﹥>﹥吖頭↗ 提交于 2021-02-11 13:42:07

问题


First, my step is just follows:

  1. Open my application(called appA), and it show the MainHome View
  2. Back to the launcher, and call another application(called appB), and click the export file to the appA
  3. And now it would call the function:
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {

My problem is how could I show the ImportHome base on the MainHome and click the back button in the ImportHome could back to MainHome?

My source code is just as follow:


class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?
    let store = Store.sharedInstance

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Set the environmentObject
        let contentView = MainHome()
            .environmentObject(store)

        showRootView(scene, rootView: contentView)
    }

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        guard let urlContext = URLContexts.first else {
            return
        }

        addSubView(scene, subView: ImportHome())
    }

    private func showRootView<T: View>(_ scene: UIScene, rootView: T) {
        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: rootView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

    private func addSubView<T: View>(_ scene: UIScene, subView: T) {
        // Todo: How to show the sub view
        let subViewUIHostingController = UIHostingController(rootView: subView)
        self.window?.rootViewController?.navigationController?.pushViewController(subViewUIHostingController, animated: true)
    }

Thank you very much!


回答1:


You need to change some state to make SwiftUI view to react and navigate to ImportHome and you need NavigationView to make navigation to & back possible.

Here is a demo of approach.

// An observable class that 
class ImportState: ObservableObject {
    @Published var urlContext: UIOpenURLContext?
    @Published var showImport = false
}

struct ImportHome: View {
    @EnvironmentObject var importState: ImportState

    var body: some View {
        Text("Do Import Here with \(importState.urlContext?.url.absoluteString ?? "<none>")")
    }
}

struct MainHome: View {
    @EnvironmentObject var importState: ImportState

    var body: some View {
        NavigationView {
            Text("MainHome Content Here")
                .background(
                    // hidden navigation link to go to ImportHome
                    NavigationLink(destination: ImportHome(),
                        isActive: self.$importState.showImport, 
                        label: {EmptyView()})
                )
                .navigationTitle("")
                .navigationBarHidden(true)
        }
    }
}

and now integrate above into SceneDelegate

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    // ... other code

    let importState = ImportState()

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Set the environmentObject
        let contentView = MainHome()
            .environmentObject(store)
            .environmentObject(importState)   // << inject
         
        // .. other code

    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        guard let urlContext = URLContexts.first else {
            return
        }

        importState.urlContext = urlContext // << store
        importState.showImport = true    // << activate navigation link
    }


来源:https://stackoverflow.com/questions/63183249/swiftui-how-could-i-show-the-new-view-base-on-the-current-scene

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!