首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI:如何在当前场景中显示新的视图基础

SwiftUI:如何在当前场景中显示新的视图基础
EN

Stack Overflow用户
提问于 2020-07-31 00:30:27
回答 1查看 247关注 0票数 0

首先,我的步骤是:

  1. 打开我的应用程序(称为appA),它将MainHome View
  2. 显示回启动程序,并调用另一个应用程序(称为appB),然后单击导出文件到appA
  3. ,现在它将调用该函数:

代码语言:javascript
复制
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {

我的问题是如何在ImportHome基础上显示MainHome,并单击ImportHome中的back按钮可以返回到MainHome?

我的源代码如下:

代码语言:javascript
复制
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)
    }

非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2020-07-31 04:21:54

您需要更改一些状态,以使SwiftUI视图响应并导航到ImportHome,并且需要NavigationView使导航到& back成为可能。

这里是一个方法的演示。

代码语言:javascript
复制
// 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中

代码语言:javascript
复制
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
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63183249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档