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

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.