Online Book Reader

Home Category

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

By Root 1043 0
happen faster (easy enough in MenuLab, because it doesn’t actually do anything). We’ll have two menu items labeled “Turbo On” and “Turbo Off,” and connect each of them to an action method in our app delegate to do the actual toggling. Then we’ll use bindings to enable and disable them as appropriate, so that when turbo is YES, only the “Turbo Off” item is clickable, and when turbo is NO, only the “Turbo On” item is clickable.

Start off in Xcode, by adding the turbo property to the app delegate, along with an action method to toggle the value of the property. The changes you need to make to both MenuLab_AppDelegate.h and MenuLab_AppDelegate.m are shown here, in a somewhat compacted form, with blank lines removed. All you need to add are the lines shown in bold.

Now switch back to MainMenu.xib in Interface Builder. Open up the nib file’s empty window, change its title to “Turbo Switch”, and drag in a checkbox from the Library, changing its title to “Turbo” once it’s in place. We put this checkbox there to give us a direct view of the value stored in the app delegate’s turbo property, so we can easily check and see that our menu items are doing the right thing. Open the Bindings

Inspector, and configure the checkbox’s Value binding, connecting it to MenuLab_AppDelegate using the turbo key path.

Next, it’s time to create some menu items (Figure 10-7 shows you what it should look like when we’re done). Double-click the MainMenu object in the nib window, so you can see the menu we’re going to add to. If any of the submenus are open, click in the menu window’s title bar to close them. Now, search for “submenu” in the Library, and drag the resulting Submenu Menu Item over to the menu, placing it between the View and Window menus. Double-click on the new top-level menu item, and change its title to Tools. Clicking on it again will reveal that it already contains a single item (titled “Item”). Click to select it, then press ⌘D to duplicate it, which places an identical item just below it.

Figure 10-7. Setting up menu items for toggling a boolean attribute

At this point, the lower of the two new menu items should be selected. Double-click to select the title text, and rename it “Turbo Off.” Ctrl-drag from it to the MenuLab_AppDelegate icon in the main window, and connect to the toggleTurbo: action. Now bring up the Bindings Inspector, and configure the menu item’s Enabled binding, connecting to MenuLab_AppDelegate using the turbo key path. This ensures that the menu will only be enabled if the value of app delegate’s turbo property is YES.

Now go back to the upper menu item, renaming it “Turbo On,” and connecting it to the app delegate’s toggleTurbo: action just like you did for Turbo Off. Because the condition for whether this menu item should be enabled or disabled is the opposite of the condition for the Turbo Off menu item, the binding is going to be a little different. It should also have its Enabled binding configured with MenuLab_AppDelegate and the turbo key path, but below that you also need to specify NSNegateBoolean as the Value Transformer.

Save your changes, go back to Xcode, and Build & Run. You should see the Turbo Switch window appear, containing a checkbox. Your app should have a Tools menu containing Turbo On and Turbo Off items, only one of which should be enabled at a time, and clicking the enabled item should toggle the checkbox and change the state of both menu items, so that now only the other item is enabled. Also, clicking the checkbox should affect the enabled/disabled state of each menu item appropriately.

You’ve now seen a simple way to enable and disable menu items through the use of bindings, but we’d be remiss if we didn’t point out that this usage is sort of artificial, and not really the way that you’d normally deal with application-wide boolean values in a menu. In a case like this, instead of two menu items, one of which is always disabled, you’re more likely to use a single menu item showing a checkbox to indicate a state, just like the checkbox in our window does. As it turns

Return Main Page Previous Page Next Page

®Online Book Reader