I have a SwiftUI (Beta 5) view with an attached ViewModel. I want to navigate to it via a navigationLink and pass in a simple parameter (called FSAC in this case)
I navigate using
NavigationLink("Next", destination: MyTestView(FSAC: "testFsac"))
The view has an FSAC Property with willSet and didSet property observer
struct MyTestView: View {
@ObservedObject var vm = MyTestViewModel()
var FSAC: String {
willSet {
print("will set fsac")
}
didSet {
print("did set fsac")
vm.FSAC = FSAC
}
}
var body: some View {
VStack {
Text("FSAC: \(FSAC)")
Text("VM FSAC: \(vm.FSAC)")
}
}
}
The print statements are never called. The first text box displays the parameter correctly; the second is blank.
How can I get the Property Observers to fire?
More generally, is there a "correct" way to use a navigationLink to pass parameters to a View that has a ViewModel?
View
? You already haveMyTestViewModel
- shouldn't things like this be done there, and just haveMyTestView
react to the model's state change? – DroopNavigationLink
- no need. But, thinking it's a SwiftUI view, I'd think you could create two "test" variables... make them@State
... and see what works. When I've done this, the@State
was the key piece. If so - and a big if since I don't knowNavigationLink
, wouldn't this mean you can use yourObservableObject
? "More generally, is there a "correct" way to use a navigationLink to pass parameters to a View that has a ViewModel?" Has this been answered but you are searching on the wrong term? I do this often - call it bullgodding! – Droop