Learn Objective-C on the Mac - Mark Dalrymple [116]
Figure 11-4. Putting the first ColorBlendView in place
Select both the ColorBlendView and the label, press ⌘D to duplicate them, and line the new ones up a bit to the right. Press ⌘D again, and line the new set up even further to the right. Now select all three ColorBlendViews and all three labels, press ⌘D, and line the new ones up below the old ones. Keep doing this until you have five rows of three labels, then go through all of the labels and set their titles, as shown in Figure 11-5.
Figure 11-5. The grid of soon-to-be-blended colors
With the layout in place, it’s time for a whole lot of connecting. You need to Ctrl-drag from the MyDocument icon in the nib window to each of the ColorBlendViews, connecting each with the outlet named similarly to the label below it.
Then it’s time to go back to MyDocument.m in Xcode. You’re going to add the code that will configure each ColorBlendView with the right blend mode, and manually configure bindings so that each ColorBlendView is updated whenever the user picks a color. Start by importing the header file for the ColorBlendView class, so that we can call its methods. Add the following line somewhere near the top of MyDocument.m:
Next, modify the windowControllerDidLoadNib: method as shown here:
The first part of that chunk of code sets the blend mode for each view, one at a time. The second part configures bindings for each ColorBlendView. Because the bindings are the same for all of them, we iterate through an array (created on the fly just before the for loop) of all the ColorBlendViews, and do the bindings for each of them.
This is really the first time we’ve asked you to do a lot of GUI configuration in code. With Cocoa’s included objects, you can configure a lot of things right in Interface Builder, but it’s not always so straightforward with your own classes. It is possible to write plugins for Interface Builder that will allow you to configure your own classes there as well, which can be a good idea in some situations, but would be overkill in this case.
Note that the calls to bind:toObject:withKeyPath: accomplish the same thing as configuring bindings in Interface Builder. Each binding you configure in Interface Builder is, in fact, more a verb than a noun. When you load a nib file containing bindings, each saved binding triggers a call just like you see here. Note also that what’s represented in Interface Builder as a Controller Key and Model Key Path, at the end of the day, are merged into a single string for the bind:toObject:withKeyPath: call.
Okay, enough theorizing for now. It’s time to save your work, Build & Run, and you should see something like Figure 11-6.
Figure 11-6. Finally, we see the blended colors.
Click on one of the color wells to open the color panel and start sliding things around in there, and you’ll see that all 15 blended colors are updated right along with the color you’re setting. Pretty slick! But the window itself still looks pretty boring.
Adding Some Background Bling
There’s a simple way to spruce this window up. We’ll put a black backdrop behind everything in the lower part of the window. In Xcode, make a new NSView subclass called BlackView, with the following content:
This simple class does nothing but draw a black rectangle over its whole area. Perfect! Now go back to Interface Builder, drag an NSView from the Library to your document window, and use the Identity Inspector to change its class to BlackView. While it’s still selected, resize it to cover everything below the color wells, then use the Layout➤Send To Back function in the menu to push it to the background of the window. Select all the labels in the window, and use the Attributes Inspector to set the foreground color for all of them to white. Finally, slide all