CodexBloom - Programming Q&A Platform

SwiftUI NavigationView causing blank screen on iOS 16 when using @State for data

👀 Views: 55 đŸ’Ŧ Answers: 1 📅 Created: 2025-05-31
swiftui navigation state swift

I'm working with an scenario with using `NavigationView` in SwiftUI on iOS 16. When I try to navigate from one view to another, the destination view appears blank. I have a `@State` variable that holds data for the destination view, and I'm trying to pass it during the navigation. Here's a simplified version of my code: ```swift import SwiftUI struct ContentView: View { @State private var selectedItem: String? var body: some View { NavigationView { List( ["Item 1", "Item 2", "Item 3"], id: \.self) { item in NavigationLink(destination: DetailView(item: $selectedItem)) { Text(item) } .onTapGesture { selectedItem = item } } .navigationTitle("Items") } } } struct DetailView: View { @Binding var item: String? var body: some View { Text(item ?? "No Item Selected") .navigationTitle("Detail") } } ``` The question arises when I tap on an item; the `DetailView` shows up but the text is blank, even though `selectedItem` has the correct value. I also tried printing `selectedItem` in the `DetailView`, but it's still showing as `nil`. I've also checked the state management and ensured that I'm using `@Binding` correctly. Additionally, I'm using Xcode 14.0 and Swift 5.7. Is there something I'm missing about how state is passed between views in SwiftUI? Any insights or best practices would be greatly appreciated!