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

Encapsulating Presentation APIs

Cocoa’s sheet and modal window presentation APIs are very flexible: you can supply your own Int as the modal response or return code. Unfortunately this leaves room for error and confusion since it can be unclear how to map an Int back to something meaningful. (Did the user click Accept or Cancel?) By tucking the sheet or modal window API usage into your window controller you can add some safety and grace to your use of them.

Consider the following code for the fictional CriticalWindowController:

c​l​a​s​s​ ​C​r​i​t​i​c​a​l​W​i​n​d​o​w​C​o​n​t​r​o​l​l​e​r​:​ ​N​S​W​i​n​d​o​w​C​o​n​t​r​o​l​l​e​r​ ​{​ ​ ​ ​ ​e​n​u​m​ ​M​o​d​a​l​R​e​s​u​l​t​:​ ​I​n​t​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​c​a​s​e​ ​A​c​c​e​p​t​ ​ ​ ​ ​ ​ ...

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