Learn Objective-C on the Mac - Mark Dalrymple [27]
NSApplication allows you to specify an optional object to act as its delegate. Simply put, a delegate is a class that handles certain tasks on behalf of another class. The application delegate allows our application to take actions at certain points in the lifecycle of the application so that we can avoid the messiness of having to subclass NSApplication.
The application delegate can be of any instance of any class, but only one object can be the application delegate. Because we already have one instance of a ButtonsAppDelegate that gets created when our application is launched, we can have that class do double-duty as our application delegate. We could also create a separate class to act as the application delegate and add an instance of that class to the nib, but it’s fairly common practice to have the application’s main window controller do double-duty as the application delegate. In fact, if you’re using Snow Leopard, the ButtonsAppDelegate class that was created for you is already configured, in the nib file, to be the application delegate, so you can skip the next paragraph.
Double-click MainMenu.xib to open up Interface Builder again. Once it’s open, Ctrl-drag from File’s Owner, which for this nib, represents our application’s one instance of NSApplication, and drag to the icon labeled Main Controller. When the grey popup menu comes up, select delegate. NSApplication has an instance variable called delegate which is an outlet, just like the ones we created in our controller class. We just connected that delegate outlet to our instance of ButtonsAppDelegate, which makes it the application delegate. Save the nib and go back to Xcode.
Configuring the Application to Quit on Window Close
Single-click on ButtonsAppDelegate.m and add the following method:
This new method is one of those special application delegate methods. At certain predefined times during the application’s run, NSApplication will look to see if its delegate has implemented a particular method. If the delegate has, NSApplication will call that method. This method we just implemented exists to let us change the behavior of NSApplication without subclassing it. The default behavior is for NSApplication to keep running until it is specifically told to quit, even if there are no windows open. It is acceptable for applications to quit when their last (or only) window is closed, however, and this method is provided specifically to let the delegate alter this behavior.
Run your application again, and close the application’s main window when it comes up. The application should now quit because the application’s only window has been closed.
Using the Documentation Browser
You might be wondering how we know what these application delegate methods are. It’s hard to implement methods if you don’t know what they are. Fortunately, they’re easy to find. If you look at the method we just added, it takes one argument, which is a pointer, to the NSApplication instance that called the method. Hold down the option key (if you’re running Leopard) or both the option and command keys (if you’re running Snow Leopard) and double-click on the word NSApplication in the editing pane. This will open up the Documentation Browser to the definition of the word we just clicked, which is NSApplication (Figure 3-18).
If you look down through the navigation pane (to the right of the blue-shaded area), you’ll see several headings, including one that reads Delegate Methods. If you click that, it will expand out to show you all the delegate methods that NSApplication supports. If you implement any of these methods in ButtonsAppDelegate, they will get called at