gpt4 book ai didi

objective-c - 清除 NSUndoManager 的重做堆栈

转载 作者:行者123 更新时间:2023-12-03 16:33:21 27 4
gpt4 key购买 nike

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

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

最佳答案

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

我能够使用 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];

关于objective-c - 清除 NSUndoManager 的重做堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5696357/

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