Reputation: 34838
When editing text within a TextField which is in a SwiftUI List there is a cursor positioning error that occurs? Any ideas how to avoid this? See video and code attached.
Note - Not sure if this matters however gcTask is a NSManagedObject (i.e. core data class instance).
UPDATE: Now confirmed this issue does directly relate to the case where the TextField string variable is tied to a Core Data class instance string attribute. In this case the core data NSManagedClass GCTask has a string attribute "title". That is why does this issue occur with my core data usage, and does not if I just used a local @State string variable?
Background: Wanting to have a simple ToDo list with the ability to click into a row and edit in-line, which you do see in some apps. Just want to understand how to do this in SwiftUI.
Code:
struct GCTaskRow : View {
@ObservedObject var gcTask: GCTask
@State var beingEdited : Bool = false
var body: some View {
HStack {
Image(systemName: gcTask.completed ? "checkmark.square" : "square")
.onTapGesture {
self.gcTask.completed.toggle()
GCCoreData.save()
}
VStack {
TextField(
"Enter:",
text: $gcTask.title,
onEditingChanged: { changed in
print("\(self.gcTask.title) => onEditingChanged = \(changed)")
self.beingEdited = changed
},
onCommit: {
print("\(self.gcTask.title) => onCommit")
// GCCoreData.save() // normally save here
}
)
}
}
}
}
Animated GIF - What happens when I click in TextField and type in 1,2,3,4,5:
Upvotes: 2
Views: 304
Reputation: 37040
The following simple test works well for me on ios 13.4.1 and catalyst. This points to gcTask as the source of the problem.
import SwiftUI
struct ContentView: View {
@State var gcTask = "xxxx"
@State var beingEdited = false
var body: some View {
List {
TextField("Enter:", text: $gcTask,
onEditingChanged: { changed in
print("\(self.gcTask) => onEditingChanged = \(changed)")
self.beingEdited = changed
},
onCommit: {
print("\(self.gcTask) => onCommit")
// where I'll put code to save/finalise update
}
)
}
}
}
Upvotes: 1