Reputation: 8547
I am having a State in SwiftUI Mac OS, which stores my active selection of a Navigation View. Everything is working with the Navigation view.
Now I have created a new class
which confirms to the Observable Object
. In some child views I will make a change to that object. When the change is done, my Navigation view updated the object aswell, which is the feature of Observable Object as far as I understand.
What I now want to achieve is that the Observable object changes my @State
in my Navigation view.
That is my declaration in my Navigation view. UserData stores a int
aswell, which should be set to the selection
on change.
@EnvironmentObject var userData: UserData
@State var selection: Int?
So userData.active = 2
, should set selection = 2
aswell. Is there a onChange event I can trigger?
I am using that @State selection
for a Binding in my Navigation Link.
NavigationLink(destination: SecondContentView(), tag: 0, selection: self.$selection)
{
Second approach, would be using that userData.active : Int
directly as State
. However, I am passing that selection
State
as Binding
and it gives me an error when passing the variable of an EnvironmentObject
as Binding
.
Upvotes: 1
Views: 432
Reputation: 258461
Try to use UserData
directly, like
NavigationLink(destination: SecondContentView(), tag: 0,
selection: self.$userData.active)
Upvotes: 1