Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to nagivate to the details screen using button. the buttons are designed in a separate view and called Using ForEach function. Please guide me to solve this.

This is the code I designed for button

struct ButtonsDesignView: View {

var buttons: MyModel.buttons
var body: some View {

        Button(action: { }) {
        ZStack {
            HStack{
                VStack{
            Text(btn.title)
                .foregroundColor(Color(.blue))
                .padding()
            
          Text(btn.subtitle)
                .foregroundColor(Color(.blue))
            .padding()
                }
                Spacer()
                VStack{
                    Image(systemName: (btn.image))
                        .resizable()
                        .scaledToFit()
                        .foregroundColor(Color(.blue))
                        .padding()
                        
                        
                }
            }
        }
    }

This is code code I've place in ContentView to call display the buttons in scrollview Using ForEach.

ScrollView(.vertical, showsIndicators: false)
                    {
                  ForEach(buttonsData) {
                item in
                        NavigationLink(destination: DetailView(buttons: item))
                    {
                            ButtonsDesignView(buttons: item)
                            .padding(10)
                            
                    }
        }.padding(.horizontal, 30)
        
            }

But with this code navigation link is not working.

question from:https://stackoverflow.com/questions/66058372/how-to-add-navigation-button-link-in-button-in-swiftui

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
227 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should add navigationLink to ButtonDesignView. I think that this should work;

NavigationLink(destination: DetailView(buttons: buttons)) {
    Button(action: { }) {
           ZStack {
               HStack{
                   VStack{
               Text(btn.title)
                   .foregroundColor(Color(.blue))
                   .padding()
               
             Text(btn.subtitle)
                   .foregroundColor(Color(.blue))
               .padding()
                   }
                   Spacer()
                   VStack{
                       Image(systemName: (btn.image))
                           .resizable()
                           .scaledToFit()
                           .foregroundColor(Color(.blue))
                           .padding()
                           
                           
                   }
               }
           }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...