【Undo Architecture】html
NSUndoManager is a general-purpose recorder of operations for undo and redo. NSUndoManager是一個一般目的操做記錄器用於實現undo和redo。app
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操做。oop
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.this
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.spa
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. rest
Redo operations and groups are simply undo operations stored on a separate stack.code
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. orm
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.htm
【Undo的註冊】blog
- (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》