Using NSUndoManager

Now that you have a general idea of how your code will interact with the undo manager, let’s look at a more concrete example. Suppose that you are writing a thermostat application. It has a property, undoManager, an instance of NSUndoManager, as well as a method, makeItHotter(), whose inverse is makeItColder(). Here is how you would implement undo for it:

f​u​n​c​ ​m​a​k​e​I​t​H​o​t​t​e​r​(​)​ ​{​
 ​ ​ ​ ​t​e​m​p​e​r​a​t​u​r​e​ ​+​=​ ​1​0​
 ​ ​ ​ ​u​n​d​o​M​a​n​a​g​e​r​.​p​r​e​p​a​r​e​W​i​t​h​I​n​v​o​c​a​t​i​o​n​T​a​r​g​e​t​(​s​e​l​f​)​.​m​a​k​e​I​t​C​o​l​d​e​r​(​)​
 ​ ​ ​ ​s​h​o​w​T​h​e​T​e​m​p​e​r​a​t​u​r​e​(​)​
}​

Two steps are performed: the prepareWithInvocationTarget(_:) method is called on the undo manager. ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.