AnnLee
AnnLee

Reputation: 11

I am not using NavigationLink and .navigationDestination correctly

I am adding an EventList to my app of CardViews but the List is not working properly. The button to access the list is at the bottom of my stack. when selected the EventList appears but selecting the chevron to navigate to the detailview gives another copy of the EventList. Selecting the back button makes the detail view appear.

The code compiles without crashing. Here is the MainView code with images of how the main view and list view appear when compiled.

struct MainView: View {
    @State private var services = [Service]()
    @State private var media = [Media]()
    @State var event: [Event] = []
    var dataService = DataService()
    var body: some View {
        NavigationStack {
            ScrollView (showsIndicators: false)  {
                VStack {
                    ForEach(services) { p in
                        NavigationLink{ Level2View(service: p) } label:
                        {
                            MainCard(service: p)
                        }
                    } // end ForEach
                    ForEach(media) { p in
                        NavigationLink{ Platform2View(media: p)
                        } label: {
                            Platform1Card(media: p)
                        }
                    }
                    NavigationLink("Go To Events"){
                        List{
                            ForEach(event) { p in
                                Section(header: Text(p.sectionTitle)
                                    .font(.title)
                                    .fontWeight(.bold)
                                ){
                                    ForEach(p.eventItem) { x in
                                        NavigationLink(value: x) {
                                            EventRow(eventItem: x)
                                        }
                                    }
                                }
                            }
                        }
                        .navigationDestination(for: EventItem.self) { x in
                            EventDetail(eventItem: x)
                        }
                        .navigationTitle("Events")
                    }
                }
            
            }// end ScrollView
            .onAppear {
            
                services = dataService.getServiceFileData()
                media = dataService.getPlatformData()
                event = dataService.getEventData()
            
            } // end onAppear
        } // end NavStack
    }
}


[Main View](https://i.sstatic.net/WuDSQHwX.png)

[EventList View](https://i.sstatic.net/TMLOfiCJ.png)

Upvotes: 1

Views: 7

Answers (0)

Related Questions