gpt4 book ai didi

swift - NSResponder 子类中的 UndoManager (macOS)

转载 作者:行者123 更新时间:2023-11-28 13:40:39 26 4
gpt4 key购买 nike

我正在尝试在我的模型中实现撤消/重做。所以我让我的模型类成为 NSResponder 的子类,然后实现了以下内容:

注意:此代码是根据评论后的更多研究编辑的

func setAnnotations(_ newAnnotations: [Annotation]) {
let currentAnnotations = self.annotations

self.undoManager.registerUndo(withTarget: self, handler: { (selfTarget) in
selfTarget.setAnnotations(currentAnnotations)
})

self.annotations = newAnnotations
}

Annotation 是一个结构。

闭包内的代码永远不会执行。最初我注意到 undoManagernil,但后来我发现了这个片段:

private let _undoManager = UndoManager()
override var undoManager: UndoManager {
return _undoManager
}

现在 undoManager 不再是 nil,但是闭包中的代码仍然没有被执行。

我在这里错过了什么?

最佳答案

既然您已经使撤消注册代码变得有意义,我就无法重现任何问题。这是测试应用程序的完整代码(我不知道 Annotation 是什么,所以我只使用了 String):

import Cocoa
class MyResponder : NSResponder {
private let _undoManager = UndoManager()
override var undoManager: UndoManager {
return _undoManager
}
typealias Annotation = String
var annotations = ["hello"]
func setAnnotations(_ newAnnotations: [Annotation]) {
let currentAnnotations = self.annotations
self.undoManager.registerUndo(withTarget: self, handler: { (selfTarget) in
selfTarget.setAnnotations(currentAnnotations)
})
self.annotations = newAnnotations
}
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let myResponder = MyResponder()
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
print(self.myResponder.annotations)
self.myResponder.setAnnotations(["howdy"])
print(self.myResponder.annotations)
self.myResponder.undoManager.undo()
print(self.myResponder.annotations)
}
}

输出是:

["hello"]
["howdy"]
["hello"]

因此撤消操作完美无缺。如果您没有遇到这种情况,则可能是您在某种程度上对“模型类”的管理不当。


顺便说一句,更正确的写法是这样的:

    self.undoManager.registerUndo(withTarget: self, handler: {
[currentAnnotations = self.annotations] (selfTarget) in
selfTarget.setAnnotations(currentAnnotations)
})

这确保了 self.annotations 不会被过早捕获。

关于swift - NSResponder 子类中的 UndoManager (macOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56078473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com