SwiftUI 文本字段控件,退出时出错
在 SwiftUI 中简单地从文本字段移动到文本字段时,我收到了多个控制台聊天项目。添加新记录或编辑现有记录时,如果我按键盘返回键(在设备上运行),我不会收到任何警告或错误。如果我只是点击另一个文本字段(不使用返回键),我会得到所有这些喋喋不休。
开始:19:45:46.969678-0700 ToDo[688:477250] [LayoutConstraints] 无法同时满足约束。以下列表中的至少一个约束可能是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的;(2) 找到添加了一个或多个不需要的约束的代码并修复它。(“”、“”、“”、“”)
将尝试通过打破约束来恢复
在 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点以在调试器中捕获它。中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能会有所帮助。2019-08-29 19:45:46.995748-0700 ToDo[688:477250] [Snapshotting] 对至少未渲染一次的视图(0x14fe87ff0,_UIReplicantView)进行快照需要 afterScreenUpdates:YES。结尾:
在这两种情况下,数据都被正确保存。我将数据存储在 Core Data.iOS 13.1,Xcode 11M392r - 网站说 Beta 7,我的 Xcode 说这个版本是 Beta 6,Catalina19A546d。
这是视图:
struct AddNewToDo: View {
@Environment(\.managedObjectContext) var managedObjectContext
@Environment(\.presentationMode) var presentationMode
@State private var updatedTitle: String = "No Title"
@State private var updatedFirstName: String = "No Title"
@State private var updatedLastName: String = "No Title"
@State private var updatedDate: Date = Date()
@State private var updatedDateString: String = "July 2019"
var body: some View {
VStack {
VStack(alignment: .leading) {
Text("ToDo Title:")
.padding(.leading, 5)
.font(.headline)
TextField("Enter a Title", text: $updatedTitle)
.onAppear {
self.updatedTitle = ""
}
.textFieldStyle(RoundedBorderTextFieldStyle())
}
.padding(10)
VStack(alignment: .leading) {
Text("First Name:")
.padding(.leading, 5)
.font(.headline)
TextField("Enter First Name", text: $updatedFirstName)
.onAppear {
self.updatedFirstName = ""
}
.textFieldStyle(RoundedBorderTextFieldStyle())
}
.padding(10)
VStack(alignment: .leading) {
Text("Last Name:")
.padding(.leading, 5)
.font(.headline)
TextField("Enter Last Name", text: $updatedLastName)
.onAppear {
self.updatedLastName = ""
}
.textFieldStyle(RoundedBorderTextFieldStyle())
}
.padding(10)
VStack(alignment: .leading) {
Text("Created Date:")
.padding(.leading, 5)
.font(.headline)
TextField("Enter a date", text: $updatedDateString)
.onAppear {
let formatter = DateFormatter()
formatter.timeZone = .current
formatter.dateFormat = "M-d-yyyy HH:mm:ss"
let myString = formatter.string(from: Date())
//let myCoreDataString = formatter.string(from: self.toDoItem.createdAt!)
//print(myString)
//print("myCoredataString is \(myCoreDataString)")
self.updatedDateString = myString
}
.textFieldStyle(RoundedBorderTextFieldStyle())
}
.padding(10)
VStack {
Button(action: ({
let nmo = ToDoItem(context: self.managedObjectContext)
nmo.id = UUID()
nmo.title = self.updatedTitle
nmo.firstName = self.updatedFirstName
nmo.lastName = self.updatedLastName
nmo.createdAt = Date()
do {
try self.managedObjectContext.save()
} catch {
print(error)
}
self.updatedTitle = ""
self.updatedFirstName = ""
self.updatedLastName = ""
self.updatedDateString = ""
self.presentationMode.wrappedValue.dismiss()
})) {
Text("Save")
}
.padding()
}
.padding(10)
Spacer()
}
}
}
任何指导将不胜感激。