简体   繁体   English

如何从 NSViewRepresentable / UIViewRepresentable 的协调器 class 中访问 EnvironmentObject?

[英]How can I access an EnvironmentObject from within a NSViewRepresentable / UIViewRepresentable 's Coordinator class?

I got an EnvironmentObject named appState, which is accessed through some of my views to share data / states.我有一个名为 appState 的 EnvironmentObject,可以通过我的一些视图访问它以共享数据/状态。 The way I use them in a view is like this:我在视图中使用它们的方式是这样的:

struct MetalView: NSViewRepresentable {
@EnvironmentObject var appState: AppState

How can I access appState from my view's Coordinator class?如何从我的视图的 Coordinator class 访问 appState?

When I try to call it in any way I've tried yet, I get this error:当我尝试以我尝试过的任何方式调用它时,我收到此错误:

"Instance member 'appState' of type 'MetalView' cannot be used on instance of nested type 'MetalView.Coordinator'" “'MetalView' 类型的实例成员 'appState' 不能用于嵌套类型 'MetalView.Coordinator' 的实例”

Any clues?有什么线索吗?

Here is how I solved this:这是我解决这个问题的方法:

AppState.swift: AppState.swift:

class AppState: ObservableObject {
    
    static let shared = AppState()
    init () {} // TODO: was private init, find out if this has benefits
    
    @Published var currentView: String = "login"
    // add rest of shared stuff below

AppDelegate.swift: AppDelegate.swift:

func applicationDidFinishLaunching(_ aNotification: Notification) {
        let appState = AppState.shared

Access from SwiftUI Views:从 SwiftUI 访问

struct ContentView: View {
    @EnvironmentObject var appState: AppState

Access from NSViewRepresentable / UIViewRepresentable Coordinator Class:从 NSViewRepresentable / UIViewRepresentable 协调器 Class 访问:

class Coordinator: NSObject, MTKViewDelegate {
...  
        func draw(in view: MTKView) {
            ...
            context.render((AppState.shared.rawImage ?? AppState.shared.rawImageOriginal)!,
                to: drawable.texture,
                commandBuffer: commandBuffer,
                bounds: AppState.shared.rawImageOriginal!.extent,
                colorSpace: colorSpace)
    }
...

This took me a lot of time to figure out, so I hope it can help some fellow beginning SwiftUI programmer...这花了我很多时间来弄清楚,所以我希望它可以帮助一些初学者 SwiftUI 程序员......

If advanced programmers can improve on this, please do so I can learn from it.如果高级程序员可以改进这一点,请这样做我可以从中学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从协调器 class 更新 EnvironmentObject 变量? - How to update EnvironmentObject variable from Coordinator class? 如何使用 Combine 跟踪 UIViewRepresentable class 中的 UITextField 更改? - How can I use Combine to track UITextField changes in a UIViewRepresentable class? 如何在辅助 Class 中正确处理 @EnvironmentObject - How to correctly handle @EnvironmentObject within secondary Class 如何将 EnvironmentObject 传递给该 EnvironmentObject 中的 ObservedObject? - How to pass an EnvironmentObject to an ObservedObject within that EnvironmentObject? 如何从类中为 EnvironmentObject 设置值? - How to set value to EnvironmentObject from class? 如何制作通用 UIViewRepresentable 结构? - How can I make a generic UIViewRepresentable struct? 协调器不适用于 UIViewRepresentable? - Coordinator is not working for UIViewRepresentable? 在 SwiftUI 中,如何访问 UIViewRepresentable 中 .foregroundColor 和其他修饰符的当前值? - In SwiftUI, how can I access the current value of .foregroundColor and other modifiers inside my UIViewRepresentable? SwiftUI:从 AppDelegate 访问 @EnvironmentObject - SwiftUI: Access @EnvironmentObject from AppDelegate 从 SwiftUI 中的 ViewModel 访问 EnvironmentObject - Access EnvironmentObject from ViewModel in SwiftUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM