I created a Model like this:
class TestModel: ObservableObject {
@Published var num: Int = 0
}
Run Code Online (Sandbox Code Playgroud)
Model is be used in "Home" view and "Home"s child view "HomeSub"
struct Home: View {
@StateObject var model = TestModel()
var body: some View {
NavigationView(content: {
NavigationLink(destination: HomeSub(model: model)) { Text("\(model.num)") }
})
}
}
Run Code Online (Sandbox Code Playgroud)
struct HomeSub: View {
//1
@StateObject var model = TestModel()
//2
@ObservedObject var model = TestModel()
var body: some View {
VStack {
Text("\(model.num)")
.padding()
.background(Color.red) …Run Code Online (Sandbox Code Playgroud)