且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SwiftUI:创建CoreData对象后无法关闭工作表

更新时间:2023-11-24 08:35:40

您的 .sheet 放置在ToolbarItem上.只需在 NavigationView 上进行更改即可.

Your .sheet is placed on the ToolbarItem. Just change it on the NavigationView and it works.

这是ContentView的正文

Here is the body of the ContentView

var body: some View {
    NavigationView {
        List {
            //Text("static text")

            ForEach(items, id:\.self) { item in
                Text("Item at \(item.timestamp!, formatter: itemFormatter)")
            }
            .onDelete(perform: deleteItems)
        }
        .navigationTitle("List of items")
        .toolbar {
            ToolbarItem(placement: .primaryAction) {
                Button(action: {
                    showingSheet = true
                }) {
                    Text("Open sheet")
                }
            }
        }
        .sheet(isPresented: $showingSheet) {
            MySheetView(isPresented: $showingSheet)
                .environment(\.managedObjectContext, viewContext)
        }

    }
}