confirmationDialog reappears before closing

Not sure what I am doing wrong. I’d like to use .confirmationDialog inside .sheet and when I press any button, it closes first, then reopens for a second and then closes again.

import SwiftUI

struct ContentView: View {
    
    @State private var sheetShow = false
    @State private var dialogShow = false
    
    var body: some View {
        NavigationStack {
            List {
                Button("Open sheet") {
                    sheetShow = true
                }
            }
            .navigationTitle("Test")
            .sheet(isPresented: $sheetShow) {
                NavigationStack {
                    List {
                        Button("Open dialog") {
                            dialogShow = true
                        }
                    }
                    .navigationTitle("Test")
                    .confirmationDialog("Title", isPresented: $dialogShow) {
                        Button("Button 1") {
                            // do something
                        }
                        Button("Button 2", role: .destructive) {
                            // do something
                        }
                    }
                }
            }
        }
    }
}

  • Maybe because it’s inside a sheet?

    – 

  • maybe, but I can see few apps doing that.

    – 




  • I think it’s incorrect UI design personally but that’s just my opinion. As for the issue, what about making the code inside the sheet into a new view and then move the state property inside that view so it’s local there in case it’s the property that is causing the issue.

    – 

  • that actually works, thanks a lot! can I ask why you think thats incorrect UI design? my use case is profile view with profile image and when you click on edit it gives you option to choose an image from the gallery, take a new photo or remove if one already exist and for that I am using confirmationDialog.

    – 

  • 1

    On the documentation page for modal presentations in Apples documentation the first sentence starts with “To draw attention to an important, narrowly scoped task…” and this sums it up pretty well why I think you shouldn’t have a modal presentation on another modal presentation because you want one thing that draws the attention, not one after another but again this is just my opinion.

    – 

Leave a Comment