清除 NSUndoManager 的重做堆栈

发布于 2024-11-02 02:57:05 字数 205 浏览 5 评论 0原文

在我的应用程序中,我想以编程方式撤消一些操作,而不向用户提供单击“重做”的选项。有没有办法清除NSUndoManager的Redo堆栈?如果没有,并且我要子类化 NSUndoManager,是否有任何方法可以访问重做堆栈以清除它?我没有从文档中看到任何方法。

或者,是否有一种方法可以恢复当前嵌套撤消组的更改而不填充重做堆栈?我已经在构建一个嵌套撤消组。

In my application, there are some actions I want to undo programmatically, without giving the user the option of clicking "Redo". Is there any way to clear the Redo stack of NSUndoManager? If not, and I were to subclass NSUndoManager, is there any way to get access to the redo stack in order to clear it? I didn't see any way to from the documentation.

Alternately, is there a way to revert the changes from the current nested undo group without it populating the Redo stack? I'm already building a nested undo group.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猥琐帝 2024-11-09 02:57:05

我最终采取了两步方法。第一步是创建一个虚拟撤消项,它会清除重做堆栈。然后,我只需删除该撤消项目,两个堆栈都是干净的。

我能够使用 self 作为虚拟撤消目标,因为我没有任何与包含代码的类关联的实际撤消操作。 self 可以替换为任何不参与撤消堆栈的对象。

诀窍是延迟调用 removeAllActionsWithTarget ,否则它不会产生任何效果。

// End the open undo grouping
[undoManager endUndoGrouping];

// Perform the undo operation, which gets pushed onto the Redo stack
[undoManager undo];

// Add a dummy Undo item to clear the Redo stack
[undoManager registerUndoWithTarget:self selector:nil object:nil];

// Remove the dummy item with a delay, pushing it to the next run loop cycle
[undoManager performSelector:@selector(removeAllActionsWithTarget:)
                  withObject:self
                  afterDelay:0.0];

I ended up taking a 2-step approach. The first step was to create a dummy undo item, which clears the Redo stack. Then, I just had to remove that undo item, and both stacks are clean.

I was able to use self as the dummy undo target, since I don't have any actual undo actions associated with the class containing the code. self could be replaced with any object that doesn't contribute to the Undo stack.

The trick was calling removeAllActionsWithTarget with a delay, otherwise it doesn't have an effect.

// End the open undo grouping
[undoManager endUndoGrouping];

// Perform the undo operation, which gets pushed onto the Redo stack
[undoManager undo];

// Add a dummy Undo item to clear the Redo stack
[undoManager registerUndoWithTarget:self selector:nil object:nil];

// Remove the dummy item with a delay, pushing it to the next run loop cycle
[undoManager performSelector:@selector(removeAllActionsWithTarget:)
                  withObject:self
                  afterDelay:0.0];
睫毛上残留的泪 2024-11-09 02:57:05
[undoManager disableUndoRegistration];
[undoManager undo];
[undoManager enableUndoRegistration];
[undoManager disableUndoRegistration];
[undoManager undo];
[undoManager enableUndoRegistration];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文