Learn Objective-C on the Mac - Mark Dalrymple [35]
In Cocoa, radio buttons are normally a group of NSButtonCell instances aligned inside an NSMatrix. You can find these preconfigured in the Library window by typing “radio” into the search field. This will show you a Radio Group. Drag this out and drop it in your window, below the other controls. The default radio button matrix contains just two radio buttons, but for this example we want five. Simply resizing the matrix, by dragging one of the resize handles, won’t help us here; that just stretches the existing buttons. Instead, try holding down the option key while dragging the bottom-center resize handle downward. That will simultaneously resize the matrix, and add new buttons inside to fill the empty space. Drag down until you can see five buttons, then release the mouse button.
TIP: You may notice at some point that many of Cocoa’s GUI classes seem to have a “shadow” in the form of a Cell class: NSButton has NSButtonCell, and so on. This division stems from performance issues in the past. In order to speed up drawing, some of the drawing duty was taken out of view classes, and put into special “cell” classes instead. So in an NSMatrix full of buttons, instead of having a bunch of full-fledged NSButton instances, there is a bunch of simpler NSButtonCell instances.
Now it’s time to fill in the values that will be displayed in each button’s title. Start with the top-most button; Double-click to select the word “Radio,” then type in “Greed.” Do the same for the rest of the buttons, typing in “Revenge,” “Bloodlust,” “Nihilism,” and “Insanity,” as shown in Figure 4-7. You’ll see that this view doesn’t expand horizontally to contain the text you typed, so do it yourself by dragging the center-right resize handle and dragging to the right until all the text you entered is visible.
Figure 4-7. What’s your primary motivation in life?
Before we’re done, we need to assign a “tag” to each cell. We can use any integer as a tag for each cell, giving us a way to reference a cell through the matrix that contains it, without needing to have an outlet for each individual cell. We want to give a unique tag to each cell in the matrix we just created, and for the sake of convenience we want them to start at zero for the uppermost cell and increment by one each step of the way down. Besides being a simple pattern to follow, this will also allow us to create an array of strings later on, whose indexes in the array perfectly match their respective cell-tags
(since an NSArray uses zero-based indexing, where the first item is at index 0, the second at index 1, and so on).
Start with the first button, “Greed.” Selecting a cell in a matrix generally requires multiple clicks. The first click selects the matrix, then a second click tells the matrix that you want to focus on its insides, and the third click actually selects the cell. Once the cell is finally selected (you can tell by the highlighting), open the Attributes Inspector (⌘1) and look for the Tag field at the very bottom. Set this to 0. Now select the “Revenge” button, and set its tag to 1. Continue down the length of the matrix, ending with setting the tag for “Insanity” to 4.
Now the radio buttons for letting the user specify a villain’s primaryMotivation are done! As you’ve done before, duplicate one of the labels, drag it down and place it above and slightly to the left of the radio button group, and change its title to “Primary Motivation:”. You should now have something similar to Figure 4-8.
Figure 4-8. More villain attributes than you can shake a stick at
Adding an Image View
No villain-tracking application