如何在 SwiftUI 中的两个视图模型之间共享已发布的模型? [英] How to share published model between two view models in SwiftUI?

查看:34
本文介绍了如何在 SwiftUI 中的两个视图模型之间共享已发布的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在两个不同的视图模型中访问同一个共享模型.两个关联的视图都需要访问视图模型中的模型并需要编辑模型.所以我不能只使用 EnvironmentObject 来访问模型.

I am trying to access the same shared model within two different view models. Both associated views need to access the model within the view model and need to edit the model. So I can't just use the EnvironmentObject to access the model.

我可以通过视图将模型传递给视图模型,但这不会使两个模型版本保持同步.有什么可以像绑定一样工作的吗?因为通过绑定,我可以访问模型,但它不会在此视图中发布更改.

I could pass the model to the view model through the view, but this wouldn't keep both model versions in sync. Is there something that could work like binding? Because with binding I can access the model but then it won't publish the changes in this view.

简化示例:

NavigationView 中的第一个视图与相邻的视图二:

First view in NavigationView with adjacent view two:

struct ContentView1: View {
    @StateObject var contentView1Model = ContentView1Model()
    var body: some View {
        NavigationView {
            VStack{
                TextField("ModelName", text: $contentView1Model.model.name)
                NavigationLink(destination: ContentView2(model: contentView1Model.model)){
                    Text("ToContentView2")
                }
            }
        }
    }
}

class ContentView1Model: ObservableObject {
    @Published var model = Model()
    //Some methods that modify the model
}

需要访问模型的相邻视图 2:

Adjacent view 2 which needs access to Model:

struct ContentView2: View {
    @StateObject var contentView2Model: ContentView2Model
    
    init(model: Model) {
        self._contentView2Model = StateObject(wrappedValue: ContentView2Model(model: model))
    }
    
    var body: some View {
        TextField("ModelName", text: $contentView2Model.model.name)
    }
}

class ContentView2Model: ObservableObject {
    
    @Published var model: Model // Tried binding but this won't publish the changes.
    
    init(model: Model) {
        self.model = model
    }
}

型号:

struct Model {
    var name = ""
}

感谢您的帮助!

推荐答案

好吧,Model 是结构体,所以从 ContentViewModel<代码>到ContentView2Model`通过

Ok, Model is struct, so it is just copied when you pass it from ContentViewModeltoContentView2Model` via

ContentView2(model: contentView1Model.model)

这种情况下,模型作为独立的 ObservableObject 更可取,因此它将通过引用从一个视图模型传递到另一个视图模型.

This is the case when it is more preferable to have model as standalone ObservableObject, so it will be passed by reference from one view model into another.

class Model: ObservableObject {
    @Published var name = ""
}

然后你可以注入它并在任何需要的子视图中修改,比如

and then you can inject it and modify in any needed subview, like

struct ContentView1: View {
    @StateObject var contentView1Model = ContentView1Model()
    var body: some View {
        NavigationView {
            VStack{
                ModelEditView(model: contentView1Model.model)       // << !!
                NavigationLink(destination: ContentView2(model: contentView1Model.model)){
                    Text("ToContentView2")
                }
            }
        }
    }
}

struct ContentView2: View {
    @StateObject var contentView2Model: ContentView2Model
    
    init(model: Model) {
        self._contentView2Model = StateObject(wrappedValue: ContentView2Model(model: model))
    }
    
    var body: some View {
        ModelEditView(model: contentView2Model.model)       // << !!
    }
}

struct ModelEditView: View {
    @ObservedObject var model: Model
    
    var body: some View {
        TextField("ModelName", text: $model.name)
    }
}

这篇关于如何在 SwiftUI 中的两个视图模型之间共享已发布的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆