swift - SwiftUI:如何从核心数据中的 TextField 中保存值
问题描述
我正在按照教程构建一个非常简单的 SwiftUI 应用程序。我创建了一个包含从 CoreData @fetched 的项目列表的视图,并且列表显示没有问题。然后我添加了一个带有 aTextField
和 a的模态窗口Button
来保存数据。这是模态的代码:
import SwiftUI
struct AddCategoryView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@Environment(\.managedObjectContext) var context
@State public var name: String = ""
@State public var id = UUID()
var body: some View {
VStack {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Close")
}
Text("Add a new Category")
.font(.largeTitle)
TextField("Enter category name", text: $name)
.padding(.all)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: {
let category = Category(context: self.context)
category.name = self.name
category.id = self.id
do {
try self.context.save()
} catch {
self.name = "There is an error!"
}
}) {
Text("SAVE \(self.name)")
}
}
}
}
struct AddCategoryView_Previews: PreviewProvider {
static var previews: some View {
AddCategoryView()
}
}
在这一行中,
Text("SAVE \(self.name)")
我正在打印(用于调试)变量名称的值,我可以看到变量更改为 catch 语句中的值
self.name = "There is an error!"
所以我知道储蓄失败了。但我不知道为什么。
在列表视图中,我有一个按钮可以打开这个模式;我改变了这个按钮的值
self.showAddModal.toggle()
至
let category = Category(context: self.context)
category.name = "New Category"
do {
try self.context.save()
} catch {
self.name = "There is an error!"
}
它有效!我可以看到列表视图已更新为该值。这是列表视图的代码
struct CategoriesView: View {
@Environment(\.managedObjectContext) var context
@FetchRequest(entity: Category.entity(), sortDescriptors: []) var categories: FetchedResults<Category>
@State private var showAddModal = false;
@State public var name = ""
var body: some View {
VStack {
Text("\(name)")
List {
ForEach(categories, id: \.id) { category in
VStack {
Text(category.name ?? "Unknown")
}
}
}
Button(action: {
let category = Category(context: self.context)
category.name = "New Category"
do {
try self.context.save()
} catch {
self.name = "There is an error!"
}
}) {
Text("+")
.font(.system(.largeTitle))
.fontWeight(.bold)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
.frame(width: 48, height: 48)
.background(Color.purple)
.cornerRadius(24)
.padding(.all)
.shadow(color: Color.black.opacity(0.3),
radius: 3,
x: 3,
y: 3)
}.sheet(isPresented: $showAddModal) {
AddCategoryView()
}
}
}
}
struct CategoriesView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
return CategoriesView().environment(\.managedObjectContext, context)
}
}
在过去的 2 个小时里,我一直在寻找和搜索解决方案,但我无法找出我的代码有什么问题。我也在模拟器上试过,但得到了同样的错误;我无法从 TextField 中保存核心数据。
提前致谢!
解决方案
好的,我终于找到了解决方案,这可能不是正确的,但至少它有效。
.sheet(isPresented: $showAddModal) {
AddCategoryView().environment(\.managedObjectContext, self.managedObjectContext)
}
是的,只需将带有上下文的环境传递给模态视图即可解决问题。我不确定这是正确的方法。
推荐阅读
- logging - LOG4J - 你能将特定的类日志记录只定向到一个附加程序吗?
- php - PHP:为什么这些公共初始化变量未定义?
- excel - 将一个单元格中的文本分隔到 Excel 中自己的字段中
- angular - 使用异步方法进行验证时,是否可以从 Angular 的 Guard 中重定向?
- javascript - onKeyup 事件调用时查找控件的 id
- java - 有人可以向我解释这个例子中的 PriorityQueue 吗?
- reactjs - materialize-css 和 Material UI 的区别
- android - 具有相同包名的两个不同项目
- android - Android 内容提供程序查询未检索到更新的信息
- sql-server - SSAS 处理错误:指定的查询太复杂,无法作为单个语句进行评估