1

是否可以以编程方式调用导航链接?

这是可能的,但现在已弃用。

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
            }
        }
4

1 回答 1

1

我不知道如何帮助调用导航链接,但也许我知道如何做你想做的事..

请记住,在 SwiftUI 中我们使用结构体,它们是static. 因此,您不能在代码运行时设置self.link1.presented?.value为。true

为此,您可以先将其更改var为 a @State var,这意味着 SwiftUI 将使此 var 动态化。考虑到这一点,您可以处理要显示的视图,只需保存标记的选项。

我修改了您的代码以显示这一点:

//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)

希望能帮助到你!

于 2019-10-20T03:44:37.953 回答