如何设置当用户点击时要执行的操作EditButton
当它显示为“完成”时?甚至可以这样做吗?
请注意,这与在用户可能执行的每个单独编辑中执行操作不同(例如onDelete
or onMove
)。当用户完成所有更改并准备好提交它们时,如何设置要执行的操作?
这是必要的,因为我正在模型的临时副本上进行所有更改,并且在用户点击“完成”之前不会将更改提交到实际模型。我还提供了一个“取消”按钮来放弃更改。
struct MyView: View {
@Environment(\.editMode) var mode
var body: some View {
VStack {
HStack {
if self.mode?.value == .active {
Button(action: {
// discard changes here
self.mode?.value = .inactive
}) {
Text("Cancel")
}
}
Spacer()
EditButton()
// how to commit changes??
}
// controls to change the model
}
}
}
是否可以在“完成”上设置“完成”操作EditButton
或者我是否必须提供自己的按钮来充当EditButton
?我当然可以做到这一点,但看起来应该很容易做到EditButton
.
XCode 12/iOS 14 的新答案使用onChange 修饰符 https://developer.apple.com/documentation/authenticationservices/signinwithapplebutton/3679061-onchange.
这种方法使用 SwiftUIeditMode
关键路径可通过@Environment
随着新的onChange
修饰符来确定视图何时进入和退出编辑模式。
在您看来,请执行以下操作:
@Environment(\.editMode) private var editMode
...
.onChange(of: editMode!.wrappedValue, perform: { value in
if value.isEditing {
// Entering edit mode (e.g. 'Edit' tapped)
} else {
// Leaving edit mode (e.g. 'Done' tapped)
}
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)