简体   繁体   English

如何通过点击 ForEach 中的按钮导航到不同的屏幕/视图

[英]How to navigate to different screens/views by tapping button which is in ForEach

I have created a dashboard with 9 buttons laid out on a lazyVGrid and a ForEach loop to display the various buttons.我创建了一个仪表板,其中包含 9 个按钮,这些按钮布置在一个lazyVGrid 和一个 ForEach 循环上以显示各种按钮。

仪表板页面
(Click to expand) (点击展开)

Now, I want to navigate to various new screens based on the button pressed.现在,我想根据按下的按钮导航到各种新屏幕。 Someone, please help me achieve this.有人,请帮助我实现这一目标。

LazyVGrid(columns: row, spacing: 25) {
                
              
                ForEach(Dashboard_Data) { data in
                    
                    Button(action: {
                                             
                            // action
                        }) {
                            
                        ZStack(alignment: Alignment(horizontal: .trailing, vertical: .top)) {
                            
                            VStack(alignment: .leading, spacing: 5) {
                                
                                Spacer(minLength: 0)

                                Text(data.data)
                                    .font(.title)
                                    .fontWeight(.bold)
                                    .foregroundColor(.white)
                                
                                HStack{
                                    Spacer(minLength: 0)
                                    Text(data.suggest)
                                        .font(.system(size: 17, weight: .regular))
                                        .lineLimit(2)
                                        .foregroundColor(.white)
                                        
                                }
                            }.padding()
                            .background(Color(data.image))
                            .cornerRadius(20)
                            .shadow(color: Color.black.opacity(0.5), radius: 5, x: 0, y: 5)
                            
                            Image(data.imageIcon)
                                .renderingMode(.template)
                                .foregroundColor(.white)
                                .font(.system(size: 25, weight: .regular))
                                .padding(10)
                                .background(Color.white.opacity(0.15))
                                .clipShape(Circle())
                        }
                        
                      
                    }
                }
                
            }
    var body: some View{
            LazyVGrid(columns: row, spacing: 25) {
                ForEach(Dashboard_Data) { data in
                    NavigationLink(destination: getTheDestination(data)) {
                        //label/design of your button
                    }
                }
            }
        }
        
        private func getTheDestination(data:<put ur dataType of Dashboard_Data> ) -> some View {
            // add some input parameter in this func to get data/informattion from your Dashboard_Data
            // according to ur data return the destination view
if data.data == "your value" {
return Text( _ data.data)
}
           return Text("Destination")
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM