SwiftUI List not maintaining selected state after navigation on iOS 17
I'm optimizing some code but I've hit a wall trying to I'm struggling with the state management of a SwiftUI `List` in my iOS 17 app. I have a list of items, and when I tap on an item to navigate to a detail view, I expect the selected item to remain highlighted when I return to the list. However, it resets every time I navigate back to the list, losing the selected state. My current implementation uses an `@State` variable to track the selected item, but it seems to be reinitialized every time the view appears. Hereβs a simplified version of my code: ```swift struct Item: Identifiable { let id: UUID let name: String } struct ContentView: View { let items: [Item] = [ Item(id: UUID(), name: "Item 1"), Item(id: UUID(), name: "Item 2"), Item(id: UUID(), name: "Item 3") ] @State private var selectedItem: Item? = nil var body: some View { NavigationView { List(items) { item in NavigationLink(destination: DetailView(item: item)) { Text(item.name) .background(self.selectedItem == item ? Color.blue : Color.clear) } .onTapGesture { selectedItem = item } } .navigationTitle("Items") } } } struct DetailView: View { let item: Item var body: some View { Text("Detail for \(item.name)") .navigationTitle(item.name) } } ``` I've also tried using an `ObservableObject` for managing the selected state, but I still faced the same scenario. After returning from the `DetailView`, the `List` simply does not remember which item was selected and displays them all in their default states. Am I missing something in terms of state persistence or the way SwiftUI handles navigation? Any insights would be greatly appreciated! I'm working on a web app that needs to handle this. Am I missing something obvious? I'm working on a service that needs to handle this. Am I missing something obvious? How would you solve this? This is my first time working with Swift 3.10. I'm working on a application that needs to handle this. Any suggestions would be helpful.