Xcode 13 测试版 5、iOS 14、macOS 11.6
我有一个列出一些孩子的父 SwiftUI 视图。每个孩子都绑定到一个NSViewRepresentable
. 一切正常,我可以按预期编辑值。但是,一旦我对列表中的项目重新排序并编辑了一个字段,它就会编辑错误的字段。看起来绑定与之前的项目订单保持不变。
看起来是这样的:
这是父母:
struct ParentView: View {
@StateObject var model = ThingModel.shared
var body: some View {
VStack{
ForEach($model.things){ $thing in
ChildView(thing: $thing)
//Reorder
.onDrag{
model.draggedThing = thing
return NSItemProvider(object: NSString())
}
}
Text("Value: \(model.value)").font(.title)
}
.frame(width:300, height: 200)
}
}
...这是子视图:
struct ChildView: View {
@Binding var thing: Thing
@StateObject var model = ThingModel.shared
var body: some View{
HStack{
GrowingField(text: $thing.text, submit: {
model.value = thing.text
print(thing.text)
})
Text(" = ")
.opacity(0.4)
}
.padding(10)
.onDrop(of: [UTType.text], delegate: ThingReorderDelegate(hoveredThing: thing))
}
}
最后,NSViewRepresentable
这就是所谓的GrowingField
. 为简单起见,我省略了NSTextField
子类。
struct GrowingField: NSViewRepresentable{
@Binding var text: String
var submit:(() -> Void)? //Hit enter
func makeNSView(context: Context) -> NSTextField {
let textField = NSTextField()
textField.delegate = context.coordinator
textField.stringValue = text
return textField
}
func updateNSView(_ nsView: NSTextField, context: Context) {
nsView.stringValue = text
context.coordinator.textBinding = $text
}
//Delegates
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, NSTextFieldDelegate {
let parent: GrowingField
var textBinding : Binding<String>?
init(_ field: GrowingField) {
self.parent = field
}
func controlTextDidChange(_ obj: Notification) {
guard let textField = obj.object as? NSTextField else { return }
self.textBinding?.wrappedValue = textField.stringValue
}
//Listen for certain keyboard keys
func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
switch commandSelector{
case #selector(NSStandardKeyBindingResponding.insertNewline(_:)):
//- Enter -
parent.submit?()
textView.window?.makeFirstResponder(nil) //Blur cursor
return true
default:
return false
}
}
}
}
为什么在NSViewRepresentable
重新排序后绑定到不跟随字段?
这是一个示例项目,可供下载并试用。