I am using the @ObservedObject PropertyWrapper for my Picker selection and I want to call a method in my View struct when the selection changes. How can I do that?
ObservableObject Code:
class SphereModel: ObservableObject {
@Published var selection = -3 {
didSet {
// Call method (getAD()) here
}
}
}
View Code:
struct ContentView: View {
@ObservedObject var sphereModel = SphereModel()
var body: some View {
Picker("Sphere Thickness", selection: $sphereModel.selection) {
ForEach((-24..<1).reversed(), id: \.self) {
Text(String(format: "%.1f", Double($0) / 2)).tag($0)
}
}
}
// Method to call
func getAD() {
}
}
How do I do that? Thanks!
You can use
onReceive
directly in your view: