zoukankan      html  css  js  c++  java
  • Undo Architecture

    Undo Architecture

      NSUndoManager is a general-purpose recorder of operations for undo and redo. NSUndoManager是一个通常目的操作记录器用于实现undo和redo。

      When you perform an action that changes the property values of an object (for example, by invoking a set accessor method), you can also register with an undo manager an operation that can reverse the action. 在设置属性时同时注册undo操作。

      An undo manager collects all undo operations that occur within a single cycle of the run loop,so that performing an undo reverts all changes that occurred during the cycle. Also, when performing undo an undo manager saves the operations that were reverted so that you can redo the undos.

      Because NSUndoManager also supports redo, these operations should typically be reversible. The method that’s invoked during an undo operation should itself register an undo operation that will then serve as the redo action.

      Undo operations are typically collected in undo groups, which represent whole revertible actions, and are stored on a stack. When an undo manager performs undo or redo, it is actually undoing or redoing an entire group of operations. 

      Redo operations and groups are simply undo operations stored on a separate stack.

      NSUndoManager normally creates undo groups automatically during the run loop. The first time it is asked to record an undo operation in the run loop, it creates a new group. Then, at the end of the loop, it closes the group. 

      Undo groups are stored on a stack, with the oldest groups at the bottom and the newest at the top. The undo stack is unlimited by default, but you can restrict it to a maximum number of groups using the setLevelsOfUndo: method. When the stack exceeds the maximum, the oldest undo groups are dropped from the bottom.

    Undo的注册

    - (void)setMyObjectTitle:(NSString *)newTitle {
     
        NSString *currentTitle = [myObject title];
        if (newTitle != currentTitle) {
            [undoManager registerUndoWithTarget:self
                    selector:@selector(setMyObjectTitle:)
                    object:currentTitle];
            [undoManager setActionName:NSLocalizedString(@"Title Change", @"title undo")];
            [myObject setTitle:newTitle];
        }
    }

      In an undo operation, setMyObjectTitle: is invoked with the previous value. Notice that this will again invoke the registerUndoWithTarget:selector:object: method—in this case with the “new” value of myObject’s title. Since the undo manager is in the process of undoing, it is recorded as a redo operation.

    Performing Undo and Redo

      The undo method is intended for undoing top-level groups, and should not be used for nested undo groups. If any unclosed, nested undo groups are on the stack when undo is invoked, it raises an exception. To undo nested groups, you must explicitly close the group with an endUndoGrouping message, then use undoNestedGroup to undo it. Note also that if you turn off automatic grouping by event with setGroupsByEvent:, you must explicitly close the current undo group with endUndoGrouping before invoking either undo method.

      An NSUndoManager object does not retain the targets of undo operations. The client—the object performing undo operations—typically owns the undo manager, so if the undo manager in turn retained its target this would frequently create a retain cycle.

      

    更多信息参考:《Introduction to Undo Architecture》

  • 相关阅读:
    命令行方式执行YUITest单元测试
    Rails系统重构:从单一复杂系统到多个小应用集群
    hudson部署设置 Spring——Java程序员的春天 ITeye技术网站
    java获取本机IP
    2.5.如何创建JAR以及把它安装到本地库中?
    管理员常用的管理工具有哪些?
    PHP学习之十四:构造函数
    window phone 7开发之 项目初体验
    window phone7开始之 横屏竖屏
    Silverlight 硬件加速
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3678578.html
Copyright © 2011-2022 走看看