O'Reilly logo

Cocoa Programming for OS X: The Big Nerd Ranch Guide by Aaron Hillegass, Adam Preble, Nate Chandler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required