If you wanted to pass a model between multiple views can you use @StateObject multiple times (see example below) so that each view can modify the object? I appreciate that this would be better done using @EnvironmentObject but was just trying to understand how this new property wrapper works in this situation.
// MARK: - MODEL
class Model: ObservableObject {
@Published var temperature = 27.5
}
.
// MARK: - APP.SWIFT
@main
struct SwiftUI_DataFlow_005App: App {
@ObservedObject var model = Model()
var body: some Scene {
WindowGroup {
ContentView(model: model)
}
}
}
.
// MARK: - CONTENT VIEW
struct ContentView: View {
@StateObject var model: Model
var body: some View {
VStack {
Text("\(model.temperature)")
AnotherView(model: model)
}
}
}
// MARK: - ANOTHER VIEW
struct AnotherView: View {
@StateObject var model: Model
var body: some View {
Text("\(model.temperature)")
}
}