Example :
import SwiftUI
struct ContentView: View {
@ObservedObject var model = ContentViewModel()
var body: some View {
// change @ObservedObject value
Text("ContentView")
.frame(width: 100, height: 100)
.background(Color.blue)
.onTapGesture {
model.isReady.toggle()
}
// change @ObservedObject value
SaleView(model: model)
// access @ObservedObject value
Text(String(model.isReady))
}
}
class ContentViewModel: ObservableObject {
@Published var isReady = true
}
struct SaleView: View {
@ObservedObject var model: ContentViewModel // can only declare, cannot initialize a new model which would be dependent.
var body: some View {
Text("change")
.frame(width: 100, height:100)
.background(Color.orange)
.onTapGesture {
model.isReady.toggle()
}
}
}