Learn Objective-C on the Mac - Mark Dalrymple [109]
Now let’s create a GUI to put this code to work. Back in MainMenu.xib in Interface Builder, drag a plain NSObject from the Library to the nib window, and use the Identity Inspector to change its class to ListWindowDelegate. Then drag out a new window from the Library, and connect its delegate outlet to the ListWindowDelegate object you’ve already got. Now find a radio group in the Library, and drag it into the new window. Including this in the window gives us a rudimentary way to provide a sort of selection for the window and its delegate. Bind the group’s Selected Tag to the ListWindowDelegate’s selectedTag property. That way, the delegate is informed whenever someone clicks one of the radio buttons. Now expand the radio group to contain more radio buttons by dragging the lower resize handle until there are seven or eight buttons. Then, click on one of the buttons near the middle, and use the Attributes Inspector to change its title to “Special Selection,” and its tag to 13013, the “magic number” that we looked for in the code.
All that’s left is configuring a new menu item to call the specialAction: method via the First Responder. Start by selecting the First Responder item in the nib window, and bringing up the Identity Inspector. Here is where we can manually add any action methods that we want this proxy object to know about. You’ll see a list of system-defined actions. Add one of your own by clicking the + button, and changing its name to specialAction: (don’t forget the colon!). Now for the menu item: find a plain menu item in the Library, and drag it into the Tools menu you created earlier. Set its title to “Special Action,” and configure its target/action by Ctrl-dragging to the First Responder item in the nib window, and selecting specialAction: from the resulting list.
Now it’s time to see this in action. Save your work, go back to Xcode, and hit Build & Run. Your app now has a new window, where you can select one of the radio buttons. Click around on some of the radio buttons, each time clicking the Tools top-level menu item to see if the Special Action item is enabled. It should only be enabled if the Special Selection button is selected. If another radio button is selected, or if the app’s other window (the old window with the Turbo checkbox) is selected, the menu item should be disabled.
This basic concept can be extended as far as you want. The point is that enabling and disabling menu items is really quite simple. You never have to manually enable or disable individual menu items as things happen in your app; you just write code that is called automatically when a menu item is about to be displayed, and deal with it then.
Sheets
The final topic we’re going to cover in this chapter is the concept of sheets. A sheet is simply a window that is temporarily attached to another window, and run in a semi-modal way, such that the “other window” doesn’t receive any events. The idea is that an application can have several windows which aren’t impacted by each other’s demands for user attention. For example, in the TextEdit application included with Mac OS X, you can have the Save As panel appearing as a sheet on one document, the Print panel appearing as a sheet on another, and continue