Home > Enterprise >  SwiftUI [Presentation] Attempt to present ... on ... whose view is not in the window hierarchy
SwiftUI [Presentation] Attempt to present ... on ... whose view is not in the window hierarchy

Time:02-22

After adding .confirmationDialog to the "Cancel" Button, I get the following message in the console, when I click on "Discard Changes" Button:

[Presentation] Attempt to present <SwiftUI.PlatformAlertController: 0x1038c8000> on <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x104c07850> (from <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVVS_22_VariadicView_Children7ElementVS_24NavigationColumnModifier_: 0x104c1b2b0>) whose view is not in the window hierarchy.

What does it mean? And is this something to worry about, or can I just ignore it? Please help

Here's my code:

import SwiftUI

struct DetailView: View {
    @Binding var scrum: DailyScrum
    
    @State private var data = DailyScrum.Data()
    @State private var isPresentingEditView = false
    @State private var isShowingConfirmationDialog = false
    
    var body: some View {
        List {
            Section(header: Text("Meeting Info")) {
                NavigationLink(destination: MeetingView()) {
                    Label("Start meeting", systemImage: "timer")
                        .font(.headline)
                        .foregroundColor(.accentColor)
                }
                
                HStack {
                    Label("Length", systemImage: "clock")
                    Spacer()
                    Text("\(scrum.lengthInMinutes) minutes")
                }
                .accessibilityElement(children: .combine)
                
                HStack {
                    Label("Theme", systemImage: "paintpalette")
                    Spacer()
                    Text(scrum.theme.name)
                        .padding(4)
                        .foregroundColor(scrum.theme.accentColor)
                        .background(scrum.theme.mainColor)
                        .cornerRadius(4)
                }
                .accessibilityElement(children: .combine)
            }
            
            Section(header: Text("Attendees")) {
                ForEach(scrum.attendees) { attendee in
                    Label(attendee.name, systemImage: "person")
                }
            }
            
            Section("History") {
                Label("No meetings yet", systemImage: "calendar.badge.clock")
            }
        }
        .navigationTitle(scrum.title)
        .toolbar {
            Button("Edit") {
                isPresentingEditView = true
                data = scrum.data
            }
        }
        .sheet(isPresented: $isPresentingEditView) {
            NavigationView {
                DetailEditView(data: $data)
                    .navigationTitle(Text("\(scrum.title)"))
                    .toolbar {
                        ToolbarItemGroup(placement: .cancellationAction) {
                            Button("Cancel") {
                                isShowingConfirmationDialog = true
                            }
                            .confirmationDialog(
                                "Are you sure you want to discard your changes?",
                                isPresented: $isShowingConfirmationDialog
                            ) {
                                Button("Discard Changes", role: .destructive) {
                                    isPresentingEditView = false
                                    // Causes: [Presentation] Attempt to present <SwiftUI.PlatformAlertController: 0x1038c8000> on <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x104c07850> (from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVVS_22_VariadicView_Children7ElementVS_24NavigationColumnModifier__: 0x104c1b2b0>) whose view is not in the window hierarchy.
                                }
                                Button("Keep Editing", role: .cancel) {
                                    isShowingConfirmationDialog = false
                                }
                            } message: {
                                Text("Are you sure you want to discard your changes?")
                            }

                        }
                        ToolbarItemGroup(placement: .confirmationAction) {
                            Button("Done") {
                                isPresentingEditView = false
                                scrum.update(from: data)
                            }
                        }
                    }
            }
        }
    }
}

CodePudding user response:

I suppose it's because by confirming the cancel you dismiss both the .confirmationDialog and the parent .sheet. I believe it doesn't hurt, but you can get rid of it like this:

            Button("Discard Changes", role: .destructive) {
                DispatchQueue.main.async {
                    isPresentingEditView = false
                }
            }

..and put it here:

    .sheet(isPresented: $isPresentingEditView) {
        NavigationView {
            Text("DetailEditView(data: $data)")
                .navigationTitle(Text("(scrum.title)"))
                .toolbar {
                    ToolbarItemGroup(placement: .cancellationAction) {
                        Button("Cancel") {
                            isShowingConfirmationDialog = true
                        }
                        
                    }
                    ToolbarItemGroup(placement: .confirmationAction) {
                        Button("Done") {
                            isPresentingEditView = false
//                          scrum.update(from: data)
                        }
                    }
                }
                // vvv Here
                .confirmationDialog(
                    "Are you sure you want to discard your changes?",
                    isPresented: $isShowingConfirmationDialog
                ) {
                    Button("Discard Changes", role: .destructive) {
                        DispatchQueue.main.async {
                            isPresentingEditView = false
                        }
                    }
                    Button("Keep Editing", role: .cancel) {
                        isShowingConfirmationDialog = false
                    }
                } message: {
                    Text("Are you sure you want to discard your changes?")
                }
        }
    }
  • Related