SwiftUI [演示] / 尝试在已经演示的...上演示视图

Mic*_*hel 20 xcode ios swiftui swiftui-list

我正在开发一个 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()
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是OtherView的代码:

import SwiftUI
import CoreData

struct OtherView: View {

    var body: some View {
        Text("Hello OtherView")
    }
}
Run Code Online (Sandbox Code Playgroud)

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

handleCustomItem(_:) 2021-04-20 22:53:10.667895+0900 TheApp[9600:5758072] [演示] 尝试在 < TtGC7SwiftUI29PresentationHostingControllerVS_7A上呈现 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView : 0x10529a510> nyView : 0x10510a310> (来自 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView : 0x10510a310> )已经呈现< TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView:0x105299150>。2021-04-20 22:53:10.668399+0900 TheApp[9600:5758072] [演示] 尝试在 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView : 0x 上呈现 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView : 0x10529b1b0> 10510a310> (来自 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10510a310> ),它已经呈现 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x105299150>。.......... 2021-04-20 22:53:10.670049+0900 TheApp[9600:5758072] [演示] 尝试在 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView 上呈现 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView : 0x105118e10> :0x10510a310 >(来自 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView : 0x10510a310>) 已经呈现 < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView : 0x105299150>。

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

ahe*_*eze 22

.sheet(isPresented: $showingOtherView) {的在里面ForEach。当您设置为 true 时,将尝试呈现showingOtherView中的所有工作表。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()
    }
}
Run Code Online (Sandbox Code Playgroud)


Big*_*air 10

我有相同的警告消息/错误,但我的原因不是像您的情况那样有太多表格。这是因为我试图太快地一张一张地呈现,如下所示:

naviViewModel.isSUModalPresented = false
naviViewModel.isPaidContentIntroPresented = true
Run Code Online (Sandbox Code Playgroud)

我必须将其更改为:

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

现在它工作得很好。SwiftUI 只需几毫秒即可完成动画。