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》

最新文章

  1. DDNS动态更新
  2. 注解学习(模仿springMvc的注解注入方式)
  3. [No00003A]操作系统Operating Systems 内核级线程Kernel Threads内核级线程实现Create KernelThreads
  4. Android自定义PopupWindow显示在控件上方或者下方
  5. js 页面值变动监听
  6. DISPLAY_ITEM built-in in Oracle D2k Forms
  7. (转)PHP中extract()函数的妙用
  8. spring bean初始化和销毁
  9. Ubuntu 12.04环境下配置Postgresql和phppgadmin
  10. 1. Hyper上的CentOS 6.5 网络配置
  11. ASP.NET没有魔法——ASP.NET MVC 与数据库之EF实体类与数据库结构
  12. NetSNMP开源代码学习——小试牛刀
  13. mapbox.gl文字标注算法基本介绍
  14. websocket ----简介,以及demo
  15. Java的MVC模式简介
  16. nginx反向代理uwsgi django服务器搭建总结
  17. 图片圆角显示与手机版文章页面CSS布局
  18. WHAT I READ FOR DEEP-LEARNING
  19. HTML5学习笔记(二十四):DOM扩展
  20. Spring MVC 重定向

热门文章

  1. Mac下忘记mysql密码怎么办
  2. js中caller和callee属性详解
  3. centos下安装docker最新版教程
  4. Apache Tomcat8必备知识(完整的支持WebSockets 1.0)
  5. 刷新SQL Server所有视图、函数、存储过程 更多 sql 此脚本用于在删除或添加字段时刷新相关视图,并检查视图、函数、存储过程有效性。 [SQL]代码 --视图、存储过程、函数名称 DECLARE @NAME NVARCHAR(255); --局部游标 DECLARE @CUR CURSOR --自动修改未上状态为旷课 SET @CUR=CURSOR SCROLL DYNAMIC FO
  6. mysql中去重 distinct 用法
  7. TCP 3-Way Handshake (SYN,SYN-ACK,ACK)
  8. python HTMLTestRunner.py
  9. CentOS 上使用vscode 调试百度大数据分析框架Apache Doris BE
  10. phpcms模块开发中的小问题及解决方法