【发布时间】:2020-05-31 15:44:38
【问题描述】:
struct ContentView {
@ObservedObject var annotationsVM = AnnotationsVM()
//I'd like to pass in the ViewModel() declared below into annotationsVM like AnnotationsVM(VModel: Vmodel)
@ObservedObjects var VModel = ViewModel()
var body: some View {
//All the SwiftUI view setup is in here
}
}
class AnnotationsVM: ObservableObject {
@ObservedObject var VModel = ViewModel()
//I'd like to pass in the VModel in content view like: @ObservedObject var VModel: VModel
}
显然,我不能像我想要的那样在创建 ContentView 时直接传入 VModel,因为尚未创建 VModel 对象,所以它无法访问...
Recap:我想将在 ContentView 中声明的 VModel 实例传入 annotationsVM 实例(也在 ContentView 中声明)
【问题讨论】:
标签: swift swiftui observableobject observedobject