Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [109]

By Root 1009 0
sort of hash of the method’s name, somewhat modified for performance considerations, which the Objective-C runtime can use to look up the method’s actual implementation. Besides looking up a method, a SEL can be compared to another SEL, such as that returned by the @selector(specialAction:) construct shown in the code. In our method, we test to see whether the menu item in question is targeted at the action method we care about, in which case we go deeper (if not, we fall through to the end of the method, where we just return YES, assuming the menu item is calling some other action method that we’re not going to worry about validating for now). If the actions match up, we then do a simple check against some internal state, in the form of the selectedTag property, to see if we want to allow this menu item to be enabled or not.

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

Return Main Page Previous Page Next Page

®Online Book Reader