【问题标题】:Dynamic NavigationLink SwiftUI Xcode动态导航链接 SwiftUI Xcode
【发布时间】:2022-01-01 05:53:35
【问题描述】:

在我的 HomeView 中,我有 2 张卡片。我制作了两个新页面(AboutA 和 AboutB)来链接到每张卡片。 但是如果我点击这张卡片,我只能预览第一页,因为我不知道如何为每个卡片设置动态导航。这是在我的 HomeView 中:

ForEach(items) { item in
    NavigationLink(destination: AboutA()) {
    CardView(item: item)
}

对于卡,我创建“数据”:

struct Item: Identifiable {
    var id = UUID()
    var title: String
    var text: String
    var image: String
}

现在我需要帮助,在 HomeView 中要更改什么,以及在“数据”页面中要更改什么。

谢谢。

【问题讨论】:

  • 欢迎来到 SO - 请使用 tour 并阅读 How to Ask 以改进、编辑和格式化您的问题。如果没有Minimal Reproducible Example,就无法帮助您进行故障排除。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: swift swiftui swiftui-navigationlink


【解决方案1】:

你有两张卡片,你必须在另一个视图中显示它们的详细信息,你称之为关于。 由于它们的性质相似,我认为关于页面也非常相似甚至相等。也许关于视图对于两个视图来说都是一样的,所以最好的办法是创建一个动态的关于视图:

struct Item: Identifiable {
    var id = UUID()
    var title: String
    var text: String
    var image: String
}

struct ContentView: View {
    @State
    var items : [Item] = [Item(title: "A", text: "test text", image: "test image"), Item(title: "B", text: "test text", image: "test image")]
    
    var body: some View {
        NavigationView {
            ForEach(items) { item in
                NavigationLink(destination: AboutView(item : item)) {
                    Text(item.text)
                }
            }
        }
    }
}

struct AboutView : View {
    var item : Item
    var body : some View {
        Text(item.title)
    }
}

因此,AboutView 对两张卡片具有相同的结构,但它会填充正确的数据。 您当然可以添加一些控件来了解是否是女巫卡,并在视图中进行编辑。

【讨论】:

    【解决方案2】:

    好的,我解决了链接卡到新页面的问题,但我遇到了新问题:)

    struct HomeView: View {
    @State var show = false
    var body: some View {
        NavigationView {
            ScrollView {
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack{
                        ForEach(items) { item in
                            NavigationLink(destination: AboutH()) {
                                CardView(item: items[0])}
                                NavigationLink(destination: Parkovi()) {
                                    CardView(item: items[1])}
                                    NavigationLink(destination: Rijeke()) {
                                        CardView(item: items[2])
                                }
                            }
                            
                        }
                    }
    

    现在我有 3 张卡片,它们是页面的链接,但是我在同一卡片的行中有树时间:(

    有什么帮助吗? 谢谢。

    【讨论】:

      猜你喜欢
      • 2020-05-28
      • 2012-06-25
      • 1970-01-01
      • 2010-10-08
      • 2020-04-09
      • 1970-01-01
      • 2021-11-28
      • 2022-01-16
      • 1970-01-01
      相关资源
      最近更新 更多