我有一个基本的基于 NSDocument 的应用程序。在以下情况下,我需要对文档进行最后一次更改:
- 用户关闭文档的窗口
- 用户终止应用程序
为什么?
文档窗口包含一个 NSTextField。通常,在用户按下 Enter(通过 )后,输入到此文本字段中的文本将提交给文档的模型textDidEndEditing(_:)
。
假设用户输入了一些文本,但没有按 Enter。相反,他按下 Cmd-W 或 Cmd-Q 来关闭文档窗口或完全终止应用程序。
textDidEndEditing
没有被调用,所以我检查文本字段是否包含更改并尝试自己更新文档。
❌ 这就是棘手的地方。以下结果导致死锁NSDocument.performActivityWithSynchonousWaiting
:
override func viewWillDisappear() {
super.viewWillDisappear()
undoManager?.disableUndoRegistration()
textField.commitEditing() // Updates the model
document.updateChangeCount(.changeDone)
}
我设法通过不挂入viewWillDisappear
,而是挂入NSDocument.canClose(withDelegate delegate: Any, shouldClose shouldCloseSelector: Selector?, contextInfo: UnsafeMutableRawPointer?)
.
✅ 此代码会在用户关闭文档窗口时保存更改:
override func canClose(withDelegate delegate: Any, shouldClose shouldCloseSelector: Selector?, contextInfo: UnsafeMutableRawPointer?) {
undoManager?.disableUndoRegistration()
textField.commitEditing() // Updates the model
updateChangeCount(.changeDone)
super.canClose(withDelegate: delegate, shouldClose: shouldCloseSelector, contextInfo: contextInfo)
}
.
❓不幸的是,当用户终止应用程序时,不会调用上述内容。我尝试更新我的文档applicationWillTerminate()
——没有用。我还尝试覆盖applicationShouldTerminate()
和延迟终止 3 秒。我可以看到文档的窗口被标记为“已编辑”,但没有保存更改。
如何在应用程序终止之前对 NSDocument 进行最后一次更改并自动保存?