将环境对象传递给NSHostingControllers

问题描述 投票:0回答:1

我正在使用自定义的SwiftUI视图,该视图使用NSSplitViewController,它为两个子视图引入了ViewBuilders。我的问题是环境状态的任何变化都不会传播到SplitView内部的子视图,而是传播到ContentView

中的另一个TextView
import SwiftUI

class AppEnvironment : ObservableObject {
    @Published var value: String = "default"
}

struct ContentView: View {
    @EnvironmentObject var env : AppEnvironment

    var body: some View {
        HStack {
            Button(action: {
                self.env.value = "new value"
            }, label: { Text("Change value") })
            Text(self.env.value)
                GeometryReader { geometry in
                    SplitView(master: {
                        Text("master")
                            .background(Color.yellow)
                    }, detail: {
                        HStack {
                            Text(self.env.value)                        }
                            .background(Color.orange)
                    }).frame(width: geometry.size.width, height: geometry.size.height)
                }
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

//
// Source: https://gist.github.com/HashNuke/f8895192fff1f275e66c30340f304d80
//
struct SplitView<Master: View, Detail: View>: View {
    var master: Master
    var detail: Detail

    init(@ViewBuilder master: () -> Master, @ViewBuilder detail: () -> Detail) {
        self.master = master()
        self.detail = detail()
    }

    var body: some View {
        let viewControllers = [NSHostingController(rootView: master), NSHostingController(rootView: detail)]
        return SplitViewController(viewControllers: viewControllers)
    }
}

struct SplitViewController: NSViewControllerRepresentable {
    var viewControllers: [NSViewController]

    private let splitViewResorationIdentifier = "com.company.restorationId:mainSplitViewController"

    func makeNSViewController(context: Context) -> NSViewController {
        let controller = NSSplitViewController()

        controller.splitView.dividerStyle = .thin
        controller.splitView.autosaveName = NSSplitView.AutosaveName(splitViewResorationIdentifier)
        controller.splitView.identifier = NSUserInterfaceItemIdentifier(rawValue: splitViewResorationIdentifier)
        let vcLeft = viewControllers[0]
        let vcRight = viewControllers[1]
        vcLeft.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 30).isActive = true
        vcRight.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 70).isActive = true
        let sidebarItem = NSSplitViewItem(contentListWithViewController: vcLeft)
        sidebarItem.canCollapse = false

        // I'm not sure if this has any impact
        // controller.view.frame = CGRect(origin: .zero, size: CGSize(width: 800, height: 800))
        controller.addSplitViewItem(sidebarItem)

        let mainItem = NSSplitViewItem(viewController: vcRight)
        controller.addSplitViewItem(mainItem)

        return controller
    }

    func updateNSViewController(_ nsViewController: NSViewController, context: Context) {
        print("should update splitView")
    }
}
swift macos swiftui appkit
1个回答
0
投票

是,在这种情况下,不会自动注入EnvironmentObject。解决方案是将内容分成指定的视图(以便进行更好的设计),然后手动注入环境对象。

这里是

Text(self.env.value)
    GeometryReader { geometry in
        SplitView(master: {
            MasterView().environmentObject(self.env)
        }, detail: {
            HStack {
                DetailView().environmentObject(self.env)
        }).frame(width: geometry.size.width, height: geometry.size.height)
    }

和视图

struct MasterView: View {
    var body: some View {
        Text("master")
            .background(Color.yellow)
    }
}

struct DetailView: View {
    var body: some View {
        HStack {
            Text(self.env.value)                        }
            .background(Color.orange)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.