I have 2 models: Bag and Food. A Bag contains 1 Food. The Food has a name.
I'm trying to bind the Food.name property with a TextField showed in the parent view (BagView) and another TextField showed in the child view (FoodView).
- When I edit the parent's text field the change is showed in bot the views.
- When I edit the child's text field the change is not showed in the parent view. Why?
Here is my sample code:
class Bag: ObservableObject {
@Published var food: Food
init(food: Food) {
self.food = food
}
}
class Food: ObservableObject {
@Published var name: String
init(name: String) {
self.name = name
}
}
struct BagView: View {
@ObservedObject var bag: Bag
var body: some View {
VStack {
TextField("Placeholder 1", text: $bag.food.name)
FoodView(food: bag.food)
}
}
}
struct FoodView: View {
@ObservedObject var food: Food
var body: some View {
TextField("Placeholder 2", text: $food.name)
}
}