【问题标题】:Clearing NSUndoManager's Redo stack清除 NSUndoManager 的重做堆栈
【发布时间】:2011-08-07 11:17:09
【问题描述】:

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

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

【问题讨论】:

    标签: objective-c cocoa undo-redo nsundomanager redo


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      [undoManager disableUndoRegistration];
      [undoManager undo];
      [undoManager enableUndoRegistration];
      

      【讨论】:

      • 这会导致异常:“使用太多嵌套的撤消组调用撤消”,无论我是否事先关闭唯一打开的撤消分组。这是否与所有这些调用都在运行循环的同一循环中有关?
      猜你喜欢
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 2023-03-27
      • 1970-01-01
      • 2011-08-13
      • 2011-10-27
      • 1970-01-01
      相关资源
      最近更新 更多