SwiftUI [演示] 尝试在...上演示...其视图不在窗口层次结构中

use*_*170 5 xcode ios swift swiftui

添加.confirmationDialog到“取消”按钮后,当我单击“放弃更改”按钮时,我在控制台中收到以下消息:

[演示] 尝试在 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView : 0x104c07850> 上呈现 <SwiftUI.PlatformAlertController: 0x1038c8000 > (来自 < TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVVS_22_VariadicView_Children7ElementVS_24NavigationColumnModifier _: 0x104c1b2b0>),其视图不在窗口层次结构中。

这是什么意思?这是值得担心的事情,还是我可以忽略它?请帮忙

这是我的代码:

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)
                            }
                        }
                    }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*isR 5

我想这是因为通过确认取消,您同时解雇了.confirmationDialog和 父级.sheet。我相信它不会造成伤害,但你可以像这样摆脱它:

            Button("Discard Changes", role: .destructive) {
                DispatchQueue.main.async {
                    isPresentingEditView = false
                }
            }
Run Code Online (Sandbox Code Playgroud)

..并将其放在这里:

    .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?")
                }
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 忽略它绝对是不安全的,而且绝对会引起问题。尝试创建警报。这是行不通的。我遇到了完全相同的问题,但尚未找到解决方案。 (3认同)