Ios Swift.sheet model不';写入核心数据后,请不要关闭
我的主视图在SwiftUI中将子视图作为.sheet模式调用。工作表中有用户填写的表单。底部的按钮用于将数据写入核心数据并关闭模式。当我在没有核心数据写入的情况下运行代码时,它会毫无问题地取消模式。当我包含核心数据代码时,它会成功地编写它,但不会取消模式。我尝试通过将@Environment合并为传递变量、使用@Binding和使用各种print语句来调试它,以确保。我没有得到任何错误,但模式从未通过按钮消失 以下是主视图的相关代码片段:Ios Swift.sheet model不';写入核心数据后,请不要关闭,ios,swift,core-data,swiftui,Ios,Swift,Core Data,Swiftui,我的主视图在SwiftUI中将子视图作为.sheet模式调用。工作表中有用户填写的表单。底部的按钮用于将数据写入核心数据并关闭模式。当我在没有核心数据写入的情况下运行代码时,它会毫无问题地取消模式。当我包含核心数据代码时,它会成功地编写它,但不会取消模式。我尝试通过将@Environment合并为传递变量、使用@Binding和使用各种print语句来调试它,以确保。我没有得到任何错误,但模式从未通过按钮消失 以下是主视图的相关代码片段: .sheet(isPresented: $showing
.sheet(isPresented: $showingSheet) {AddListItem(listName: "", favoriteFlag: false)
下面是显示模式的子视图的完整代码:
struct AddListItem: View {
@State var listName: String
@State var favoriteFlag: Bool
@Environment(\.presentationMode) var presentationMode
@Environment(\.managedObjectContext) private var viewContext
var body: some View {
VStack {
Form {
Section(header: Text("List Details")) {
TextField("List Name", text: $listName)
Toggle(isOn: $favoriteFlag) {
Text("Favorite?")
}
}
}
HStack {
Button(action: {
withAnimation {
let newItem = Item(context: self.viewContext)
newItem.timestamp = Date()
do {
try self.viewContext.save()
self.presentationMode.wrappedValue.dismiss()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
//self.presentationMode.wrappedValue.dismiss()
}) {
Text("Add Tasks to \(listName)")
}
}
.navigationBarTitle("Create New List")
}
}
}
我在这里回顾了一些关于堆栈溢出的类似问题,但没有找到解决方案。我觉得我错过了一些简单的事情。。。谢谢你的帮助 啊!我想出来了。。。多亏了这个答案 我一开始没看到。如果它对其他人有帮助,我就犯了将.sheet附加到工具栏项的错误。它应该是分开的。一旦我做到了,就没问题了