Reputation: 1328
Is it possible to call a Navigation Link programmatically?
It was possible, but now deprecated.
var link1 = NavigationLink("Link Text", destination: MyView1())
var link1 = NavigationLink("Link Text", destination: MyView2())
//Something like this
Button(action: {
if (option == 1){
self.link1.presented?.value = true
}
else{
self.link2.presented?.value = true
}
}
Upvotes: 1
Views: 417
Reputation: 515
I don't know how to help with calling a Navigation Link but maybe I know the way to do what do you want..
Remember in SwiftUI we use structs, and they are static
. For this reason you cannot set self.link1.presented?.value
to true
while the code is running.
To do that, you can first change this var
to a @State var
which means SwiftUI will make this var dynamic. With this in mind, you can handle which view you will show, simply saving which option is marked.
I modified your code to show this:
//begining of your struct
@State var selectedOption = 1
//something changed the selectedOption
self.selectedOption = 2
//Here your NavigationLink ( use it without button)
NavigationLink("Link Text", destination: selectedOption == 1 ? View1 : View2)
Hope it helps!
Upvotes: 1