Learn Objective-C on the Mac - Mark Dalrymple [55]
Make the window a bit wider than its default size, in order to accommodate the controls we’re about to add. Press ⌘3 to bring up the Size Inspector, and make sure that your new window is selected by clicking its title bar, then type 530 into the Width field.
The final step is to hook things up so that a user can open this window by accessing the Preferences item in the menu. Double-click the MainMenu object in the main nib window, and navigate to the Preferences item. This object will connect to the Preferences window with target/action, so Ctrl-drag from the menu item to the Preferences window (either drag to the icon representing the window in the main nib window, or to the title bar of the real window), and select the makeKeyAndOrderFront: action. That’s a method that will make any window become the front-most window in its application, and also become the “key” window, ready to receive events in response to the user pressing keys on the keyboard (hence the “makeKey” in the method name).
Add a Tab View
We’re going to split up the application preferences into different three different groups, one for each of the three game objects the user can create. We’ll separate the groups by placing the controls an NSTabView, which lets the user switch between different views by selecting from a list of tabs along the top. In the Library window, type in “tab,” then drag the resulting Tab View into your empty window. Resize it so that it nearly fills the window (as shown in Figure 6-1), leaving the standard margin that the blue lines will happily show you.
Figure 6-1. The preferences window for DungeonThing looks like this after the creation and naming of the tabs
When you drag out a new tab view from the Library, by default it has two tabs, but we want ours to have three. Click the tab view, and open the Attributes Inspector (⌘1). There you will see a Tabs field. Click the tiny up-arrow to change its value from 2 to 3, and you’ll see that there are now three tabs. Double-click on the title of each tab, and change their titles to “Character Generation,” “Monster Generation,” and “Dungeon Generation,” respectively. Figure 6-1 shows what your window should look like at this point.
Character Generation Preferences
Now let’s start populating these tabs with some controls. First up is the Character Generation tab, so click on that tab to select it. By now you should be comfortable enough finding views and controls in the Library that we won’t hold your hand and tell you every single step of grabbing these objects and dragging them into your window. It should be just about enough for you to look at Figure 6-2 and know that we’re using a slider, a matrix of radio buttons, a matrix of checkboxes, and a handful of text fields for labels, including the small one out to the right of the slider.
Figure 6-2. Here are the preferences for character generation in DungeonThing.
Drag out each of these objects from the Library and align them roughly as you see in Figure 6-2. The only class there that you may not have used already is NSSlider, which works a lot like the NSLevelIndicator that we used in Chapter 4. To make it look and act like the one seen in the image, select it and open the Attributes Inspector (⌘1). Give it 19 tick marks, check the box to make it stop only on tick marks, set its minimum and maximum values to 2 and 20, and