When creating a class conforming to ReferenceFileDocument
, how do you indicate the document needs saving. i.e. the equivalent of the NSDocument
's updateChangeCount
method?
I've met the same problem that the SwiftUI ReferenceFileDocument
cannot trigger the update. Recently, I've received feedback via the bug report and been suggested to register an undo.
Turns out the update of ReferenceFileDocument
can be triggered, just like UIDocument
, by registering an undo action. The difference is that the DocumentGroup
explicitly implicitly setup the UndoManager
via the environment.
For example,
@main
struct RefDocApp: App {
var body: some Scene {
DocumentGroup(newDocument: {
RefDocDocument()
}) { file in
ContentView(document: file.document)
}
}
}
struct ContentView: View {
@Environment(\.undoManager) var undoManager
@ObservedObject var document: RefDocDocument
var body: some View {
TextEditor(text: Binding(get: {
document.text
}, set: {
document.text = $0
undoManager?.registerUndo(withTarget: document, handler: {
print($0, "undo")
})
}))
}
}
I assume at this stage, the FileDocument
is actually, on iOS side, a wrapper on top of the UIDocument
, the DocumentGroup
scene explicitly implicitly assign the undoManager to the environment. Therefore, the update mechanism is the same.