问题描述
我有一个具有managedObjectContext(环境变量)的视图.当在带有NavigationLink的NavigationView中显示时,它可以正常工作.但是,当视图以表格形式显示时,它不起作用.保存某些实体数据时出现错误.
I have a view that has managedObjectContext(Environment Variable). It works fine when presented in NavigationView with NavigationLink. But it does not work when the view is presented as a sheet.It gives me an error when I save some entity data.
操作无法完成.(Foundation._GenericObjCError错误0.)"
"The operation couldn’t be completed. (Foundation._GenericObjCError error 0.)"
使用环境对象时是否缺少任何内容.我的代码
Is there anything I am missing when using the Environment object. My code
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
@Environment(\.managedObjectContext) var managedObjectContext
推荐答案
工作表不是 ContentView
的子组件.
因此,您必须将此环境分别传递给工作表:
Thus, you have to pass this Environment separately to the sheet:
.sheet(isPresented: self.$isPresented, content: MySheetView().environment(\.self.moc)
其中 moc
在启动工作表的视图中定义为:
where moc
is defined in the View that initiates the sheet as:
@Environment(\.managedObjectContext) var moc
这篇关于出现工作表时,EnvironmentObject不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!