- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的模型中实现撤消/重做。所以我让我的模型类成为 NSResponder
的子类,然后实现了以下内容:
注意:此代码是根据评论后的更多研究编辑的
func setAnnotations(_ newAnnotations: [Annotation]) {
let currentAnnotations = self.annotations
self.undoManager.registerUndo(withTarget: self, handler: { (selfTarget) in
selfTarget.setAnnotations(currentAnnotations)
})
self.annotations = newAnnotations
}
Annotation
是一个结构。
闭包内的代码永远不会执行。最初我注意到 undoManager
是 nil
,但后来我发现了这个片段:
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/
HTML5 规范提到了一个 UndoManager,它可用于查询用户编辑过的文档的先前状态 (http://www.whatwg.org/specs/web-apps/current-work/#un
我使用 UndoManager在我的 Swing 应用程序中。如果在 UndoManager 上调用了 undo()、redo()、addEdit() 或其他方法,Undo 和 Redo 按钮最终必须
我有 3 个 MOC。 MainThread MOC 显示内容(使用 undomanager) 后台保存 MOC 以将数据保存到光盘(连接到存储) Backgorund-update MOC 从服务器
我正在使用 UndoManager 来捕获我的 JTextArea 中的更改。 setText() 方法会删除所有内容,然后粘贴文本。当我撤消时,我首先看到一个空白区域,然后它会显示它之前的文本。 如
我对 ACE 很陌生编辑器和 javascript 一般情况下,除了以下内容之外,我已经设法实现了大部分我想要的: 我想启用或禁用“保存”按钮,具体取决于是否对文档进行了未完成的修改,并且我已尝试通过
我希望能够从我的文档模型中访问 UndoManager,这样我就可以在模型中注册撤消操作: // Assume I've extended MyDocument to conform to Refer
我有一个名为 appointment 的 NSManagedObject,我编辑了它的属性。如果我的用户按下取消,我想撤消所有这些编辑。 如果我这样做(示例代码) [[appointment mana
使用 ACE Editor , 它看起来好像使用 editor.getSession().getUndoManager() 和一个已经用 editor.setSession() 设置的 session
我有一个 JTextArea 和一个 JComboBox 允许我循环浏览各种打开的文件 - JTextArea 的内容随着我的选择而改变一个不同的文件。我试图为每个文件维护一个不同的撤消缓冲区,并为每
我正在尝试在我的模型中实现撤消/重做。所以我让我的模型类成为 NSResponder 的子类,然后实现了以下内容: 注意:此代码是根据评论后的更多研究编辑的 func setAnnotations(_
我正在努力向使用 undoManager 的 Swift 库添加一些测试。在库的演示中,将以下打印添加到 viewDidAppear: 会导致打印 NSUndoManagerObject。 overr
我对如何在 Firefox 中使用 undoManager 有点困惑。根据Mozilla , Firefox 20 现在支持它,但我不知道在哪里。我已经在 document、Objects 等下进行了
我想在我的应用程序中应用撤消和重做操作。我阅读了苹果文档并用谷歌搜索了它,但没有得到正确的解决方案。在我的应用程序中,我在 UIImage 上应用了很多图像操作,我想存储每个状态并相应地应用 Undo
我将以下代码用于核心数据单例。下面是我的代码。 (基于 NachoMan 的 blog 。但是代码来自他的要点。 // DataManager.h #import #import extern N
如何在 Swift 中使用 UndoManager(以前的 NSUndoManager)? 这是我尝试复制的 Objective-C 示例: [[undoManager prepareWithInvo
我正在尝试在类似绘画的程序中设置UndoManager,但不幸失败了。我一直在查看的示例程序是文本编辑器 ( Example ),它们调用 JTextComponent 类的 addUndoableE
我尝试使用 undomanager 撤消/重做编辑顶点。 图形对象经过测试。但我不知道该怎么做编辑顶点撤消/重做。 顶点是否可以撤消/重做? 我找了很多例子都没有找到答案。 我是韩国初学者程序员。帮帮
使用 Swift 3.02 (Xcode 8.2) 我引用了答案中描述的撤消解决方案:https://stackoverflow.com/a/31450475/7448394 . 在下面的代码中,我使
我只是试图让用户在我的应用程序中执行的操作撤消。默认情况下,用户所做的任何文本编辑都具有撤消的好处,但以其他方式(通过我的代码)完成的任何操作都没有。 我可以看到文档解释说我需要获取 NSUndoMa
为什么@Environment UndoManager不更新其 canUndo当它的堆栈中有 Action 时的属性?我有一个 View ,它有一个可以使用取消/重做功能的 child ,但由于某种原
我是一名优秀的程序员,十分优秀!