I have a view that includes a List and associated TextFields . Adding to SwiftData works but the update does not . Used @Binding, .onChange etc to make sure the @State var changed . Tracked with print () to before the update with all changes to the TextField verified. However SwiftData doesn’t keep the changes , stays with the original data.
Really confused as to where the problem is .
Working back from the 'Save ’ ;
Button("Save",
action: {
lab.company = labName
lab.telephone = telephone
lab.email = email
lab.contact = contact
if !editMode {
context.insert(lab)
} else {
print(lab.company,lab.telephone,lab.email,lab.contact)
try? context.save()
}
dismiss()
})
VStack(alignment: .leading) {
TextField("Lab Name", text: $labName)
.textFieldStyle(.automatic)
.focused($foc)
.onChange(of: labName){ oldValue, newValue in
labName = newValue
}
TextField("Contact", text: $contact)
.textFieldStyle(.automatic)
.onChange(of: contact){ oldValue, newValue in
contact = newValue
}
List (labs, id: .self) { lab in
//ForEach(labs) {lab in
HStack {
Text (lab.company)
.font(.caption)
.frame(width: 150)
.onTapGesture {
loadText(lab: lab)
foc = true
}
```` @Environment(\.modelContext) private var context
@Environment(\.dismiss) private var dismiss
@Query var labs: [LabInfo]
@Bindable var lab: LabInfo
@State private var labName: String = ""
@State private var contact: String = ""
@State private var telephone: String = ""
@State private var email: String = ""
@State private var editMode: Bool = false
@FocusState private var foc: Bool
@Model
class LabInfo {
var id: UUID
@Attribute(.unique) var company: String
var contact: String
var telephone: String
var email: String
init(id: UUID, company: String, contact: String, telephone: String, email: String) {
self.id = id
self.company = company
self.contact = contact
self.telephone = telephone
self.email = email
`
Another although minor issue is when the focus is placed on the first edit field it is at the end of the test . Is there away of placing it at the beginning.
TIA
Joel
![Simulator Screenshot - iPad Air 11-inch (M2) - 2024-06-06 at 09.08.17|690x479](upload://6XtLHbweG3sjxnNgo8eYIajNl6n.jpeg)