SwiftUI NavigationLink loads destination view immediately, without clicking

前端 未结 4 1769
不知归路
不知归路 2020-12-05 13:01

With following code:

struct HomeView: View {
    var body: some View {
        NavigationView {
            List(dataTypes) { dataType in
                Na         


        
4条回答
  •  被撕碎了的回忆
    2020-12-05 13:34

    The best way I have found to combat this issue is by using a Lazy View.

    struct NavigationLazyView: View {
        let build: () -> Content
        init(_ build: @autoclosure @escaping () -> Content) {
            self.build = build
        }
        var body: Content {
            build()
        }
    }
    

    Then the NavigationLink would look like this. You would place the View you want to be displayed inside ()

    NavigationLink(destination: NavigationLazyView(DetailView(data: DataModel))) { Text("Item") }
    

提交回复
热议问题