Online Book Reader

Home Category

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

By Root 945 0
would be complete without a way to display the villain’s cruel visage. In VillainTracker, we’ll use an instance of the NSImageView class both to display a picture of the villain, and to receive a picture as input via the Mac’s drag-and-drop mechanism.

Go back to the Library window, and type “image” in the search field. One of the results is listed as an Image Well, and this is in fact an instance of NSImageView. Drag one of these over to your window, and place it a bit to the right of the matrix of radio buttons from the last section.

Now, that image well is pretty tiny, so let’s make it bigger. Bring up the Size Inspector (⌘3), and you’ll see in a pair of text fields that the image well’s width and height are both 48. Edit each of those, changing them to 104, and you’ll see the control’s size in the window change. We want to let the user drag an image into this view, so you’ll also need to bring up the Attributes Inspector (⌘1) and click to enable the Editable checkbox,

Now adjust the position of the image well by dragging it a little within its window. You should see the blue guidelines pop up, and notice as you bring it near the radio buttons from the last step that this that control is now the same vertical height as the matrix of radio buttons to its left; Put them side by side, then duplicate another label, put it a bit above and to the left of the image view, and change its title to “Appearance:”. See Figure 4-9 for a rough idea of the layout at this point.

Figure 4-9. An image well is now in place.

Adding Checkboxes in a Matrix


The next controls we want to add are a series of checkboxes to let the user check off all the powers and abilities that the villain has (stored in the powers attribute). As in the case of the villain’s primary motivation, we have come up with a classification system in which all types of supervillain powers can be pigeonholed into a category. Once again, we will use a matrix, but this time filled with checkboxes instead of radio buttons.

Go the Library window again, and this type “check” into the search field. One of the results will be labeled Check Box. Grab one of those and pull it into your window. This places an NSButton, configured to work as a checkbox, into the window. We’re going to need 12 of these, lined up in a matrix, but right now we’ve only got this single one. Time for you to learn about a handy shortcut in Interface Builder that lets you automatically take any object that can appear in an NSMatrix, such as an NSButton, and replace it with a new NSMatrix containing a cell configured the same as your original object. Simply select your object (the checkbox), then in the menu choose Layout➤Embed Objects In➤Matrix. Your checkbox is now wrapped up in a matrix, and you can use the same resizing trick you learned for the matrix containing the radio buttons: hold down option, click on the bottom-center resize handle, and drag downwards until you see 12 checkboxes.

Next, let’s configure this new matrix to work correctly with a group of checkboxes. Select the matrix itself (not one of the checkboxes inside of it), and open the Attributes Inspector (⌘1). At the top of the Inspector window, you’ll see a popup button labeled “Mode”, probably showing “List” as its value. Change that to “Highlight” instead, so we get the correct behavior.

Now fill the checkbox titles with the following: Strength, Intellect, Psionics, Imperviousness, Speed, Stealth, Fighting Ability, Time Control, Cosmic Consciousness, Size, Special Weapon Attack, Leadership. While clicking around to set these values, you may inadvertently turn checkboxes on and off. Don’t worry about that; the states of all those checkboxes will be set from the code itself.

Just like with the matrix of radio buttons, you’ll need to make the matrix a little wider so that the full text of the buttons can be seen. Drag the center-right resize handle out until all the text is visible.

Now it’s time to set tags for all of these checkboxes, just like you set tags for all the radio buttons for choosing the primaryMotivation.

Return Main Page Previous Page Next Page

®Online Book Reader