2011-04-17 39 views
2

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

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

回答

1

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

我能够使用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]; 
1
[undoManager disableUndoRegistration]; 
[undoManager undo]; 
[undoManager enableUndoRegistration]; 
+0

这造成了一个例外:“撤消叫了太多的嵌套撤消群体”,我是否不关闭只开放撤销事先分组。这是否与所有这些调用都处于运行循环的相同循环中有关? – Dov 2011-04-18 19:42:01