SwiftUI [Presentation] / 尝试在已经呈现的 ... 上呈现视图

SwiftUI [Presentation] / Attempt to present View on ... which is already presenting

我正在开发一个 SwiftUI 应用程序,首先收到警告,然后收到错误,这可能是因为我忽略了这些警告。我想显示我收到的警告,我希望有人能指出我可能做错的事情。

相关代码如下:

struct CustomListView: View {
    var localList:[SomeManagedObject], moc:NSManagedObjectContext
    @State var showingOtherView = false
    
    func handleCustomItem(_ argument: SomeManagedObject) {
        print(#function)
        self.showingOtherView.toggle()
        ..... Do useful things .....
    }
    
    var body: some View {
        List {
            ForEach(self.localList) {
                item in
                HStack {
                    Spacer()
                    Button(action: {
                        self.handleCustomItem(item)
                    })
                    {
                        Text(item.expression!)
                            .foregroundColor(Color.red))
                            .font(.headline)
                            .padding(.horizontal, 11).padding(.vertical, 15)
                    }.sheet(isPresented: $showingOtherView) {
                        OtherView()
                    }
                    Spacer()
                }
            }
        }
    }
}

这是 OtherView 的代码:

import SwiftUI
import CoreData

struct OtherView: View {

    var body: some View {
        Text("Hello OtherView")
    }
}

这些是我在调试控制台中看到的消息,当我单击列表中的一个按钮并执行 handleCustomItem() 函数时:

handleCustomItem(_:) 2021-04-20 22:53:10.667895+0900 TheApp[9600:5758072] [Presentation] Attempt to present <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10529a510> on <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10510a310> (from <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10510a310>) which is already presenting <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x105299150>. 2021-04-20 22:53:10.668399+0900 TheApp[9600:5758072] [Presentation] Attempt to present <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10529b1b0> on <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10510a310> (from <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10510a310>) which is already presenting <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x105299150>. ........... 2021-04-20 22:53:10.670049+0900 TheApp[9600:5758072] [Presentation] Attempt to present <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x105118e10> on <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10510a310> (from <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10510a310>) which is already presenting <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x105299150>.

如果有人能看到一些东西并给我提示我可以做什么,那就太好了。

您的 .sheet(isPresented: $showingOtherView) {ForEach 内。当您将 showingOtherView 设置为 true 时,ForEach 中的所有工作表都会尝试呈现。那是一大堆床单。

您想把它放在 ForEach 之外。

var body: some View {
    List {
        ForEach(self.localList) {
            item in
            HStack {
                Spacer()
                Button(action: {
                    self.handleCustomItem(item)
                })
                {
                    Text(item.expression!)
                        .foregroundColor(Color.red))
                    .font(.headline)
                    .padding(.horizontal, 11).padding(.vertical, 15)
                }
                Spacer()
            }
        }
    }
    .sheet(isPresented: $showingOtherView) { /// here should be fine
        OtherView()
    }
}

我收到了同样的警告 message/bug,但我的原因并不像您的情况那样 sheet 太多。是因为我试图像这样太快地依次呈现一个 sheet:

naviViewModel.isSUModalPresented = false
naviViewModel.isPaidContentIntroPresented = true

我必须更改为:

naviViewModel.isSUModalPresented = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { // or even shorter
     naviViewModel.isPaidContentIntroPresented = true
}

现在可以完美运行了。 SwiftUI 只需几毫秒即可完成动画。