Online Book Reader

Home Category

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

By Root 941 0
Backspace or Delete to clear out the text.

Let’s top this box off by adding the button that will eventually put some text into the text field we just created. Find a button in the Library (a Push Button will do nicely), and drag it out to a spot just above the box. During the drag, the blue lines will appear, showing you the correct distance from the top of the window, and also showing you when the button is lined up directly above the center of the box. That’s where you want to drop it! Then double-click the button to edit its title, and change it to “Generate Character.” Figure 6-8 shows what you should be seeing at this point.

Figure 6-8. We’ve created the first of three sets of views for the main window. The text field inside the box is highlighted, just so you can see where it is.

Now select all the views you just created, by clicking anywhere in the window and then pressing ⌘A to select all objects in the window. Duplicate them by pressing ⌘D, which will show you a new set of identical objects, overlapping and slightly position-shifted from the originals. Drag this cluster to the center of the window, using the blue guidelines to make sure that they are at the same vertical position at the originals, and with just the right horizontal spacing between the boxes. Then press ⌘D again, and drag the third cluster of objects to the right side of the window, again using the blue guidelines to help position them properly. Finally, double-click on the two new buttons, changing their titles to “Generate Monster” and “Generate Dungeon” respectively. Figure 6-9 shows what the window should look like now.

Figure 6-9. Here’s what the completed main window for DungeonThing looks like.

Set Up the DungeonThingAppDelegate


Now that the initial set of bindings are out of the way and the main window is all set up, we can go ahead and create an instance of our DungeonThingAppDelegate. Go the Library window, search for “nsobject,” and drag the resulting Object to your main nib window. Then bring up the Identity Inspector (⌘6) and change the new object’s Class to “DungeonThingAppDelegate.” If you’re running Snow Leopard, you can skip that step, since the app delegate is already included in the nib file that’s created for you.

One at a time, Ctrl-drag from the DungeonThingAppDelegate to each of the three labels (actually NSTextFields) in the three boxes, connecting them to the characterLabel, monsterLabel, and dungeonLabel outlets. Then Ctrl-drag from each of the three buttons back to DungeonThingAppDelegate, connecting each to the relevant DungeonThingAppDelegate action methods, createCharacter:, createMonster:, and createDungeon:.

Define Your Constants


At this point, the GUI is completed, and the relevant bindings are all configured so that the controls in the Preferences window will all have their values saved in the user’s application preferences. All that’s left is for us to write the action methods that will use NSUserPreferences to retrieve the preferences values, and display them. Like we mentioned before, we’re stopping short of actually using the values to generate game item descriptions, but feel free to make that enhancement later on, if you’d like.

Let’s start off by defining some constants, just like we’ve done in previous examples (if it’s slipped your mind, see Chapter 4 for a discussion of the benefits of doing this). Here are the constants to match all the values you’ve already set in for the preferences’ key names in the nib file. Insert these somewhere at the top of DungeonThingAppDelegate.m:

NOTE: In order to fit the confines of the book format while still showing valid code, we’ve “wrapped” some of these lines by putting a backslash as the very last character of a line, which makes the C preprocessor just tack on the content of the next line, as if it were all on the same line to begin with. Feel free to omit this manual wrapping in your own code, making each of those #defines into a one-line declaration.

Specify Default Preferences Values


With the constant definitions

Return Main Page Previous Page Next Page

®Online Book Reader