2

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

或者,有没有办法在不填充重做堆栈的情况下恢复当前嵌套撤消组的更改?我已经在构建一个嵌套的撤消组。

4

2 回答 2

4

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

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

诀窍是延迟调用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];
于 2011-04-19T12:46:57.960 回答
2
[undoManager disableUndoRegistration];
[undoManager undo];
[undoManager enableUndoRegistration];
于 2011-04-17T21:03:39.843 回答