SwiftUI:如何在当前场景下显示新的View

时间:2020-07-31 00:30:27

标签: swiftui

首先,我的步骤如下:

  1. 打开我的应用程序(称为appA),它显示MainHome视图
  2. 返回启动器,并调用另一个应用程序(称为appB),然后单击将文件导出到appA
  3. 现在它将调用该函数:
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {

我的问题是如何显示MainHome上的ImportHome基础,然后单击ImportHome中的后退按钮可以返回MainHome?

我的源代码如下:


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)
    }

非常感谢您!

1 个答案:

答案 0 :(得分:0)

您需要更改某些状态以使SwiftUI视图做出反应并导航至ImportHome,并且需要NavigationView才能进行往返导航。

这是方法的演示。

// 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)
        }
    }
}

现在将其集成到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
    }