我怎样才能显示基于当前场景的新视图

我怎样才能显示基于当前场景的新视图

本文介绍了SwiftUI:我怎样才能显示基于当前场景的新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的步骤如下:

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

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

我的源代码如下:

类 SceneDelegate: UIResponder, UIWindowSceneDelegate {变量窗口:UIWindow?让商店 = Store.sharedInstancefunc 场景(_ 场景:UIScene,willConnectTo 会话:UISceneSession,选项 connectionOptions:UIScene.ConnectionOptions){//设置环境对象让 contentView = MainHome().environmentObject(商店)showRootView(场景,rootView:contentView)}func 场景(_ 场景:UIScene,openURLContexts URLContexts:Set){守卫让 urlContext = URLContexts.first else {返回}addSubView(scene, subView: ImportHome())}private func showRootView(_scene: UIScene, rootView: T) {//使用 UIHostingController 作为窗口根视图控制器.如果让 windowScene = 场景为?UIWindowScene {让窗口 = UIWindow(windowScene: windowScene)window.rootViewController = UIHostingController(rootView: rootView)self.window = 窗口window.makeKeyAndVisible()}}private func addSubView(_scene: UIScene, subView: T) {//Todo: 如何显示子视图让 subViewUIHostingController = UIHostingController(rootView: subView)self.window?.rootViewController?.navigationController?.pushViewController(subViewUIHostingController,动画:true)}

非常感谢!

解决方案

您需要更改一些状态以使 SwiftUI 视图做出反应并导航到 ImportHome 并且您需要 NavigationView 导航至 &可能回来.

这是方法的演示.

//一个可观察的类类导入状态:ObservableObject {@Published var urlContext: UIOpenURLContext?@Published var showImport = false}struct ImportHome:查看{@EnvironmentObject var importState: ImportStatevar主体:一些视图{Text("Do Import Here with \(importState.urlContext?.url.absoluteString ?? "")")}}struct MainHome:查看{@EnvironmentObject var importState: ImportStatevar主体:一些视图{导航视图{文本(这里的主要主页内容").背景(//隐藏的导航链接转到 ImportHomeNavigationLink(目的地:ImportHome(),isActive: self.$importState.showImport,标签:{EmptyView()})).navigationTitle("").navigationBarHidden(真)}}}

现在集成到SceneDelegate中

class SceneDelegate: UIResponder, UIWindowSceneDelegate {//... 其他代码让 importState = ImportState()func 场景(_ 场景:UIScene,willConnectTo 会话:UISceneSession,选项 connectionOptions:UIScene.ConnectionOptions){//设置环境对象让 contentView = MainHome().environmentObject(商店).environmentObject(importState)//<<注入//.. 其他代码func 场景(_ 场景:UIScene,openURLContexts URLContexts:Set){守卫让 urlContext = URLContexts.first else {返回}importState.urlContext = urlContext//<<店铺importState.showImport = true//<<激活导航链接}

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!

解决方案

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
    }

这篇关于SwiftUI:我怎样才能显示基于当前场景的新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:37