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
}
}
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 setself.link1.presented?.value
totrue
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:
Hope it helps!