4 ms·
This is how NSUndoManager works in Cocoa, with the additional twist that recording the reverse operation is performed by actually invoking it on a proxy. Examp
by ridiculous_fish 9y ago
This is how NSUndoManager works in Cocoa, with the additional twist that recording the reverse operation is performed by actually invoking it on a proxy.
Example:
- (void)setTitle:(NSString *title) {
[[NSUndoManager prepareWithInvocationTarget:self]
setTitle:currentTitle_];
currentTitle_ = title;
}
This setTitle: method may be undone by calling setTitle: again with the old title. We actually send that to the undo manager, which records it.
When the user invokes undo, the undo manager replays the message it was sent. setTitle again records how to undo the replayed operation, but this time it gets pushed onto the redo stack!
What is nice about this approach is there is no need to manually reify commands (i.e. Command pattern). It's just a message send.