SwiftUI view state resetting after alert is shown

Seeing an issue in iOS 26.2 iPhone 17 simulator (haven't been able to reproduce on device or other simulators), where a view's state is reset after an alert is shown.

In this example the first LibraryView has the issue when alert is shown, the second LibraryView maintains state as expected.

struct ContentView: View {
    var body: some View {
        NavigationStack {
            List {
                VStack {
                    LibraryView(title: "Show view (Loss of state)")
                }
                
                LibraryView(title: "Show view (Works as expected)")
            }
        }
    }
}

/// This view is from a package dependency and wants to control the presentation of the sheet internally
public struct LibraryView: View {
    @State private var isPresented: Bool = false
    let title: String
    
    public init(title: String) {
        self.title = title
    }
    
    public var body: some View {
        Button(self.title) {
            self.isPresented = true
        }
        .sheet(isPresented: self.$isPresented) {
            ViewWithAlert()
        }
    }
}

private struct ViewWithAlert: View {
    @State private var isPresented: Bool = false
    @State private var presentedCount = 0
    
    var body: some View {
        Button("Show Alert, count: \(presentedCount)") {
            isPresented = true
            presentedCount += 1
        }
        .alert("Hello", isPresented: self.$isPresented) {
            Button("OK") { }
        }
    }
}

Any ideas?

The issue can be corrected by moving the .sheet to a higher level within the layout (i.e. on the NavigationStack). However, the library wants to control that presentation and not require the integration to present the sheet.

Thank you for sharing your post.

I find it intriguing, but I am unable to replicate the functionality you have described on my own devices. The code works the same in all simulators I have tried.

The parent SwiftUI view is a List, which is destroyed when nested view presentations and alerts are involved. While you mentioned that it works correctly on other simulators and devices, I have consistently encountered the same issue when running it on different simulators with the same configuration.

I would personally implement a custom mechanism to manage when and how the sheet is presented, potentially using a combination of state flags and conditional views to avoid relying on the immediate modifier behavior or better, do not include inside a List that will be destroyed.

I am still very interested in knowing what device and versions of iOS the code:

  List {
                VStack {
                    LibraryView(title: “Show view (Loss of state)”)
                } }

works so that I can investigate the reason, in my case, the state did not work in any simulator because the List was destroyed.

Thanks,

Albert Pascual
  Worldwide Developer Relations.

SwiftUI view state resetting after alert is shown
 
 
Q