SwfitUI列表如何动态推送到其他视图

时间:2020-05-22 07:01:10

标签: list swiftui navigationlink

  let datasource = [
    "view1",
    "view2",
    "view3",
    "view4",
    "view5",
    "view6",
    ...
  ]

我想在列表中显示数据源,并推送到其他视图,例如View1(),View2(),View3(),View4()...

我应该在SwiftUI中做什么?

1 个答案:

答案 0 :(得分:0)

这里是example on how to programmatically decide to which view you want to go。这也可以用在列表中...

struct ProgrammaticNavigationExampleView: View {

    enum NavDestination {
        case red
        case green
    }

    @State var destination : NavDestination?

    var body: some View {
        VStack(spacing: 20) {
            NavigationLink(destination: Color.red, tag: NavDestination.red, selection: $destination) {
                EmptyView()
            }
            NavigationLink(destination: Color.green, tag: NavDestination.green, selection: $destination) {
                EmptyView()
            }
            Text("Page 1")
            Button("Show random page") {
                self.destination = [.red, .green].randomElement()!
            }
        }
        .navigationBarTitle("Page 1")
    }

}

struct ProgrammaticNavigationExample_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            ProgrammaticNavigationExampleView()
        }
    }
}