Learn Objective-C on the Mac - Mark Dalrymple [37]
At this point, you should now have something similar to Figure 4-10.
Figure 4-10. Preparing for the worst of the worst
Configuring a Popup Button
Now we’ll deal with the villain’s powerSource, using a popup button to let the user select from a predefined list in order to specify how the villain got his powers. Once again we’ve narrowed it down to a few choices: Innate (the villain was born with it), Freak Accident (some “act of god” such as a lab explosion, meteor strike, or similar, caused a transformation), Superhero Action (a superhero in the course of their heroics accidentally empowers someone else), and Other.
Go to the Library window again, and enter “popup” into the search field. Grab a Pop Up Button from the search results, and drag it into your window, placing it a bit below the checkbox matrix from the previous section. By clicking and then double-clicking on the new popup button, you can see the default list of values that can be selected. Double-click on the first one to edit its text, changing it to “Innate.” Continue for the rest of the titles, entering the names mentioned previously. After entering the third one, you’ll see that you’re out of button values; the default popup button only has three values. Click on one of the three values, duplicate it by pressing ⌘D, and then enter the final value. Now grab another label, put it in place to the left of this popup button, and change its title to “Source of Powers:”.
Inserting a Text View
Now it’s time for the final control, a text view for editing free-form notes about the villain in question. We’ll be using NSTextView, a powerful class for text display and editing that is used throughout the Mac OS X interface. In the Library window, enter “text view,” and drag the resulting text view into your window, below all the other elements. Resize it to fill most of the available space. Your window layout should now look something like Figure 4-11.
Figure 4-11. All the controls are now in the window.
Making Logical Groupings
Now that all the controls we need are laid out in the window, let’s take a step back and see where we stand. Figure 4-11 is probably similar to the interface you have right now.
That may be functional, and clearly labeled, but it’s not going to win us any design awards, that’s for sure. Our interface exposes a lot of functionality, but the controls are all sort of jammed together, and it’s hard to see any logical groupings of all these editable attributes. We can improve things a bit by grouping related controls inside of “box” views; Cocoa provides us with a class called NSBox that can contain several other objects and draw a nice border around them.
Interface Builder contains a nice shortcut that lets us put existing views into a box perfectly sized to contain them all precisely within its borders, so it’s really easy to add this enhancement to our interface. To start, select the first five controls (in the upper left-hand of your window) and all of their labels. The easiest way to do this is to click in the upper left corner of your window, just below the window’s close/minimize/maximize controls, and drag a selection box around all the views you want to select. Then, choose Layout➤Embed Objects In➤Box from the menu, and you’ll see that a nice box surrounds them, with a darker gray background color and rounded corners. At the top of the box is a text label (“Box”) that can be edited to something descriptive of the box’s contents. Double-click that text, and change it to “Basic Information.”
Go on and do the same thing for the Primary Motivation section. First, delete the label by selecting it and pressing Backspace or Delete. Because there’s only one set of controls in the box we’re going to create, we can just use the box’s title instead. Now select