在我的应用程序中,有一些操作我想要以编程方式撤消,而不是让用户选择单击“重做”。有没有办法清除NSUndoManager的重做堆栈?如果不是,并且我是NSUndoManager的子类,有没有办法访问重做堆栈以清除它?我在文档中看不到任何方法。
或者,有没有一种方法可以在不填充重做堆栈的情况下恢复当前嵌套撤消组中的更改?我已经在构建一个嵌套的撤销组了。
发布于 2011-04-19 20:46:58
我最终采取了两步走的方法。第一步是创建一个虚拟的撤销项,用于清除重做堆栈。然后,我只需要删除那个撤销项,两个堆栈都是干净的。
我能够使用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];发布于 2011-04-18 05:03:40
[undoManager disableUndoRegistration];
[undoManager undo];
[undoManager enableUndoRegistration];https://stackoverflow.com/questions/5696357
复制相似问题