Online Book Reader

Home Category

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

By Root 936 0
an NSComboBox in Interface Builder

After adding a list of superheroes, test your interface by selecting File➤Simulate Interface from the menu, or by pressing ⌘R. You’ll see your window appear, and you can verify that the items you created for the combo box appear when you click on it. When you’re done, press ⌘Q to return to Interface Builder.

Indicating a Rating with a Level Indicator


The next control we’re going to create is an NSLevelIndicator, which we’ll use to display and specify the evilness of the villain we’re displaying. We’ll let the user specify a “star rating” from 0 to 10 for this value, and the NSLevelIndicator will actually display it as a row of stars.

Go back to the Library window, and type in “level” in the search field at the bottom. One of the classes you’ll see is NSLevelIndicator, which you should drag out and put into your window, once again using the blue guidelines to line it up with the left edge of the previous controls, just below the combo box you added in the previous step.

Now we need to configure this new object a bit. The NSLevelIndicator class has a number of built-in display styles, suited for different purposes. The default style for a new instance created in Interface Builder is called “Discrete”, but we’re going to change this to match what we’re displaying, which is a sort of a rating. Bring up the Attributes Inspector (⌘1), and you’ll see that one of the first options is a popup labeled Style (see Figure 4-6). Click on the popup and select Rating instead. You’ll see that the level indicator immediately changes to a view showing a few stars.

Figure 4-6. Configuring an NSLevelIndicator to show a star rating

We also need to specify the maximum and minimum values that are valid for this control, so that it knows how to display the value it’s given. Still in the Attributes Inspector, you’ll see a section labeled Values, containing controls to set the minimum and maximum values. Set the minimum to 0 and the maximum to 10, either by typing numbers into these fields or using the small up/down buttons. A bit below those text fields is another labeled Current. Set that to 10, so that we can see the full range of stars in our control, and click the Editable checkbox so that the user can actually edit this value themselves, not just look at the pretty stars.

You may now notice that the control isn’t showing you 10 stars. As of this writing, the default size of a new NSLevelIndicator dragged from the Library is wide enough to show just over 7 stars, and no more. Fortunately, Interface Builder provides a handy short cut that can help you “right-size” a view to make it snugly fit around its content. Select Layout Size To Fit from the menu or press ⌘= to make the level indicator expand to just the right size to show all 10 stars.

Finally, it’s time once again to duplicate one of the existing labels in your window. Drag it into place to the left of the level indicator, and change the text to “Evilness:”.

This might be a good time to once again test your interface by pressing ⌘R. You should be able to click anywhere along the line of stars and cause the control to start highlighting just that amount.

Adding Radio Buttons in a Matrix


Next, we’ll create the GUI for the primaryMotivation attribute. Years of careful study of comic books have taught us that every villain has a primary motivating factor that causes them to do their evil deeds, and this motivation can always be reduced to one of these five things: greed, revenge, bloodlust, nihilism, or insanity. Of course some villains may have several of these evil-making forces at work, but we’ll settle for picking the main one for each villain we’re tracking.

We’re going to use a set of radio buttons to let the user pick the primary motivation. Radio buttons are generally grouped in a set that works together, so that pressing one button to select it simultaneously deselects all the other buttons. If “radio button” seems like a terribly obscure name for this, then perhaps you’re too young to remember the old car-radio

Return Main Page Previous Page Next Page

®Online Book Reader