Hi, I'm facing a problem where if I click a NavigationLink and then go back to that page, the item I just clicked stays selected. It's been pretty frustrating. Here is my code! Thanks :)
Code Block swift struct EventsListView: View { @EnvironmentObject var viewModel: OlympicGame @State var showEditEvent: Bool = false @State var showAddCustomPoints: Bool = false var body: some View { VStack { GeometryReader { geometry in VStack { List { Section(header: VStack(alignment: .leading) { Text("Open:").font(.headline) } ) { ForEach(self.viewModel.eventsList) { event in if event.completed == false { NavigationLink(destination: EventInProgressView(event: event) .environmentObject(viewModel)) { EventsListItemView(event: event) .foregroundColor(Color.black) .environmentObject(viewModel) }.listStyle(DefaultListStyle()) } }.onDelete(perform: delete) }.textCase(nil) if self.viewModel.eventsList.filter { $0.completed == true }.count > 0 { Section(header: VStack(alignment: .leading) { Text("Completed:").font(.headline) }) { ForEach(self.viewModel.eventsList.filter { $0.completed == true }) { event in NavigationLink(destination: EventInProgressView(event: event).environmentObject(viewModel)) { EventsListItemView(event: event) .foregroundColor(Color.black) .environmentObject(viewModel) }.listStyle(PlainListStyle()) } } } if self.viewModel.eventsList.isEmpty { VStack { Spacer() Image("SalmonLeaps") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 300, height: 300) .padding(.vertical, 40) } } } VStack { if self.viewModel.eventsList.filter { $0.completed == false }.isEmpty && self.viewModel.eventsList.filter { $0.completed == true }.count > 0 { finishView() } pointsView(for: geometry.size) } } } } .navigationBarTitle("Events") .navigationBarItems( trailing: Button(action: { self.showEditEvent.toggle() }) { EventsListNewItemView() } .padding() .sheet(isPresented: $showEditEvent) { NewEventView(isShowing: $showEditEvent) .environmentObject(viewModel) } ) } func finishView() -> some View { NavigationLink(destination: EndScreenView().environmentObject(viewModel)) { Text("🏅 Tap here to finish! Or create more events!").foregroundColor(.accentColor) } } func pointsView(for size: CGSize) -> some View { VStack(alignment: .leading) { Text("Point Totals").font(.headline).padding(.horizontal, 30) HStack { HStack { Circle() .fill(viewModel.team1.color) .frame(width: teamScoreCircleRadius(for: size), height: teamScoreCircleRadius(for: size)) .shadow(radius: 1) Text(String(viewModel.team1.points)) .font(.system(size: teamScoreFontSize(for: size))) }.padding() HStack { Circle() .fill(viewModel.team2.color) .frame(width: teamScoreCircleRadius(for: size), height: teamScoreCircleRadius(for: size)) .shadow(radius: 1) Text(String(viewModel.team2.points)) .font(.system(size: teamScoreFontSize(for: size))) }.padding() Spacer() Button(action: { self.showAddCustomPoints.toggle() }) { Image(systemName: "plus").imageScale(.large).padding(.horizontal) } .foregroundColor(.accentColor) .padding() .sheet(isPresented: $showAddCustomPoints) { AddCustomPoints(isShowing: $showAddCustomPoints) .environmentObject(viewModel) }.buttonStyle(PlainButtonStyle()) }.padding(.horizontal) } } func teamScoreCircleRadius(for size: CGSize) -> CGFloat { let x = min(size.height, size.width) return x/15 } func teamScoreFontSize(for size: CGSize) -> CGFloat { let x = min(size.height, size.width) return x/20 } func delete(at offsets: IndexSet) { print(offsets) viewModel.removeEvent(at: offsets) } }