Online Book Reader

Home Category

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

By Root 895 0
if need be) and type in the word “label.” Now you should see an object called Label. Drag one of those out to your window, dropping it to the left of the first text field you created. Again, you’ll notice the blue lines that pop up when you drag near another object, helping you guide it into place. This label will resize horizontally to match its content when you edit it within IB. That’s fine, except that by default the text is centered horizontally, which means that while you are editing it will expand in both directions and start overlapping the NSTextField to its right. To counteract that, open the Attributes Inspector (⌘1) and find and click the align right button (hint: it looks just like the corresponding button found in most word processors). Doing so will make the label retain its right-side margin even when you change its content.

Edit the text in the label to say “Name:” and then make a copy of this label by pressing ⌘D. Drag the new label down a bit so that it lines up with the lower text field, and then edit it to say “Last Known Location:” and you’re done!

Letting them Pick Dates


The next GUI object we want to prepare is a date picker, which will let the user specify the date of the villain’s last sighting. A date picker overcomes the ambiguities and competing standards for date formatting that can be a chore to deal with when parsing and presenting dates, and we’ve got access to a great built-in one in Cocoa, so let’s roll with it.

In the Library window’s search field, type “date picker” and see what comes up. You should see an entry called Date Picker. Grab that, drag it into your window, and drop it below the NSTextFields you created earlier, once again using the blue guidelines that pop up as you drag near the spot you’re looking for. The default width for this control is smaller than the default width for the NSTextFields above it, so it won’t take up the same amount of horizontal space, but that’s okay. Just make it line up with the left edge of the other controls. Now click on one of the labels you created earlier and make another copy of it by pressing ⌘D. Drag it down to line up with your date picker, and change the text to “Last Seen:”. Figure 4-4 shows what your window should look like now.

Figure 4-4. We’re off to a good start adding controls and labels to our window.

Creating the Combo Box


Now it’s time for a combo box. This control is great if you have a predefined set of valid string values to choose from, but also want to allow the user to enter a value of their own. This is perfect for the villain’s swornEnemy property. We can provide some default superhero names to choose from, while still allowing the user to enter one that we don’t anticipate.

Go to the Library window and type “combo” into the search field, and you’ll see a Combo Box appear in the list. Drag it out into your window, below the NSTextFields and the NSDatePicker. Use the blue guidelines once again, lining it up with the left edge of the controls above it. Once more, make a copy of one of the existing labels, position it to the left of the combo box, and rename this one “Sworn Enemy:”.

Now, let’s populate the combo box with some default values. We want the user to be able to specify any superhero at all, even ones we don’t know about (that’s why we chose the combo box), but for the sake of convenience we should preload this control with names of some popular superheroes, since the popular ones also seem to be the ones who attract the most enemies.

Click on the combo box you just added, and open the Attributes Inspector (⌘1). In the upper section, you’ll see a spot labeled Items where you can add items that will appear in the combo box. Figure 4-5 shows this part of the Inspector in action. Use the + button to add a new item, then double-click the “New Item” text that appears, and change it to “Superman.” Now repeat this process, using the + button to add several items, each time editing the “New Item” text and changing it to the name of one of your favorite superheroes.

Figure 4-5. Configuring

Return Main Page Previous Page Next Page

®Online Book Reader