SwiftUI NavigationLink not preserving state on tab switch in iOS 17
Quick question that's been bugging me - After trying multiple solutions online, I still can't figure this out... I'm relatively new to this, so bear with me. I'm experiencing an issue with `NavigationLink` in SwiftUI where the state of my views does not persist when switching between tabs in a `TabView`. Specifically, when I navigate to a detail view and then switch to another tab, the detail view resets to its initial state when I return. I've tried using `@State` and `@ObservedObject` to manage the state but it still resets. Here's a simplified version of my code: ```swift struct ContentView: View { @State private var selectedTab = 0 @State private var selectedItem: Item? var body: some View { TabView(selection: $selectedTab) { NavigationView { List(items) { item in NavigationLink(destination: DetailView(item: item, selectedItem: $selectedItem)) { Text(item.name) } } .navigationTitle("Items") } .tabItem { Text("Items") } .tag(0) Text("Other Tab") .tabItem { Text("Other") } .tag(1) } } } struct DetailView: View { var item: Item @Binding var selectedItem: Item? var body: some View { Text(item.details) .onAppear { self.selectedItem = item } } } ``` In this setup, I expected that the `DetailView` should maintain its state while navigating between tabs, but it seems like the entire navigation stack is being recreated. I’ve also tried using an `@EnvironmentObject` to manage the state globally, but that didn't help either. Could this be due to the way SwiftUI handles `NavigationView` within `TabView`? Are there any recommended practices to ensure that the state persists across tab switches? Any insights would be greatly appreciated! Has anyone else encountered this? I'm working on a CLI tool that needs to handle this. I'm working on a service that needs to handle this. Any ideas what could be causing this? What's the correct way to implement this?