CodexBloom - Programming Q&A Platform

Trouble with SwiftUI and NavigationLink implementation guide view with state changes in iOS 17

👀 Views: 15 💬 Answers: 1 📅 Created: 2025-08-28
SwiftUI iOS NavigationLink Swift

I've been banging my head against this for hours... I've looked through the documentation and I'm still confused about I'm using SwiftUI to build my application, and I’ve encountered a frustrating scenario with `NavigationLink`. When navigating between views, the destination view doesn't update with the new state changes unless the entire view is refreshed. I have the following simplified structure: ```swift struct ContentView: View { @State private var selectedItem: Item? var body: some View { NavigationView { List(items) { item in NavigationLink(destination: DetailView(item: item)) { Text(item.name) } .simultaneousGesture(TapGesture().onEnded { selectedItem = item }) } } } } struct DetailView: View { var item: Item var body: some View { Text(item.details) // Some more UI elements } } ``` The question arises because when I update `selectedItem`, I expect the `DetailView` to reflect the changes, but it doesn’t. Instead, if I navigate back and forth, the view will sometimes update, but often it shows outdated information. I've tried using the `onAppear` modifier in `DetailView` to refresh the content: ```swift .onAppear { // Code to refresh the view } ``` But it still doesn’t seem to work consistently. I’ve also checked that the `Item` struct conforms to `Identifiable` and `Hashable`. Is there a better way to handle state updates when using `NavigationLink`? Any insights would be greatly appreciated! What am I doing wrong? Am I approaching this the right way?