【问题标题】:Swiftui NavigationLink not executingSwiftui NavigationLink 未执行
【发布时间】:2021-07-30 13:08:08
【问题描述】:

我正在尝试在 LazyVGrid 中使用 NavigationLink,但我在导航方面遇到了一些麻烦。我希望在单击颜色时可以继续到下一页。这是一些代码:

这是起始页的外观here

以下是图像卡的设置方式:

    ZStack{
        Color(colorData.image)
            .cornerRadius(15)
            .aspectRatio(contentMode: .fit)
            .padding(8)
            .matchedGeometryEffect(id: colorData.image, in: animation)
    }

下面是 LazyVgrid 的设置方式:

            ScrollView(.vertical, showsIndicators: false, content: {
                
                VStack{
                    
                    LazyVGrid(columns: Array(repeating: GridItem(.flexible(),spacing: 15), count: 2),spacing: 15){
                        
                        ForEach(bags){color in
                            
                            ColorView(colorData: color,animation: animation)
                                .onTapGesture {
                                    withAnimation(.easeIn){
                                        print("pressed")
                                        selectedColor = color
                                    }
                                }
                        }
                    }
                    .padding()
                    .padding(.top,10)
                }
            })
        }

这是我的导航方式:

        if selectedColor != nil {
        
            NavigationLink(destination: DetailView()) {}
            
        }

【问题讨论】:

  • 你能再显示一些代码吗? selectBag 在哪里定义?您也可以尝试将 ColorView 包装在 NavigationLink 到 DetailView 中。

标签: swiftui swiftui-navigationview


【解决方案1】:

您可以按照@workingdog's answer 中的说明进行操作,或者像这样稍微修改代码:

struct ContentView: View {
    @State var isActive: Bool = false
    @State var selectedColor: Color?

   var body: some View {
   NavigationView {
    NavigationLink(destination: DetailsVieww(selectedColor: selectedColor), isActive: $isActive) {
            EmptyView()
    }
    VStack {
            ColorView(colorData: color, animation: animation)
                .onTapGesture {
                selectedColor = color
               isActive = true
            }
        }
        }
    }
}

【讨论】:

    【解决方案2】:

    通常使用导航你会做这样的事情:

    struct ContentView: View {
        @State var selection: Int?
        @State var selectedColor: Color?
        
        var body: some View {
            NavigationView {  // <--- required somewhere in the hierarchy of views
                // ....
                NavigationLink(destination: DetailsVieww(selectedColor: selectedColor),
                               tag: 1,
                               selection: $selection) {
                    ColorView(colorData: color, animation: animation)
                        .onTapGesture {
                            withAnimation(.easeIn) {
                                selectedColor = color
                                selection = 1   // <--- will trigger just the tag=1 NavigationLink
                            }
                        }
                }
                // ....
            }
        }
    }
    
    struct DetailsVieww: View {
        @State var selectedColor: Color?
        
        var body: some View {
            Text("selected color view \(selectedColor?.description ?? "")")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 2020-02-08
      • 2021-03-27
      • 2020-04-11
      相关资源
      最近更新 更多