I’ve still had no luck resolving this. Interestingly the EnvironmentObject is updating instantly on the “VarityListVIew” where the user selects the different quantity and variety of each component. However when the back button is pressed returning to the “ComponentListView” the updated EnvironmentObject is not carried over, when a new version of “ComponentListView” is loaded (by going back and clicking the link again) the EnvironmentObject is updated as expected. Any ideas greatly received!
I have put the code for the “VarietyListView” and a GIF of the above scenario below.
struct VarietyListView: View {
@EnvironmentObject var model:FoodandDrinkModel
@EnvironmentObject var model2:VarietyModel
@State var component:Component
@State var selectedVariety:Int = 0
@State var selectedQuantity:Int = 0
var thisVariey:[Varieties] { model.getVarities(component: component, varietyModel: model2)
}
var body: some View {
HStack{
Text (String(component.name))
}
.onAppear {selectedQuantity = component.quantity}
.onChange(of: selectedQuantity, perform: { value in
component.quantity = selectedQuantity
print (selectedQuantity)
})
.onAppear {selectedVariety = component.varietyCalories}
.onChange(of: selectedVariety, perform: { value in
component.varietyCalories = selectedVariety
print (selectedVariety)
})
Spacer()
Text("Choose quantity:")
Picker(selection: $selectedQuantity, label: Text("Choose quantity")) {
ForEach(1..<30) { index in Text(" \(index)0g").tag(index)
}
}
.pickerStyle(WheelPickerStyle())
Text("Choose variety:")
Picker(selection: $selectedVariety, label: Text("Choose variety")) {
ForEach (thisVariey) { r in
Text(r.name).tag(r.calories)
}
}.pickerStyle(WheelPickerStyle())
Spacer()
VStack {
HStack{
Text("Calories:")
Text(String(model.calculatedCalories(component: component, computedQuantity: selectedQuantity, computedCalories: selectedVariety)))
}
}
}
}