Online Book Reader

Home Category

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

By Root 1046 0
nib has been loaded. Here we use the objectController to create a new model object, set values for its two attributes, and add the new object to the controller.

With that in place, all that’s left to do is to connect MyDocument’s objectController outlet within Interface Builder. So switch back over there, Ctrl-drag from the File’s Owner to the colorset object controller, and select the objectController outlet. Save your work, go back to Xcode, Build & Run. You should now see red and yellow colors in the two color wells, and clicking on one of them should bring up the color panel and let you change the color.

Not only that, but you’ll now find that a full complement of document functionality is available through the menu. You can save your documents, close them, access recent documents, open documents, and so on.

Settling on a File Format


Having gotten to this point, you’ve probably noticed that when you save a document, a popup list lets you choose to save the file as Binary, SQLite, or XML, and the file extension is set accordingly. While this level of flexibility may be of some use during development, for a shipping product you should really just pick a format and stick with it, to avoid confusing your users. In general, SQLite is probably the best choice for most applications. You should also change the file extension to something that suggests the use of your app, instead of the default extension. All of these options are configured in Xcode’s Target Inspector. Go back to Xcode, open the Targets group in the navigation pane, click on the ColorMix target, and press ⌘I to open the Inspector. Select the Properties tab, and the lower half of the panel shows you a table containing all three pre-configured file formats (labeled here as “Document Types”); Select and delete the Binary and XML options (by using the “-” button at the table’s lower left-hand corner), leaving only the SQLite option. Give this remaining format a decent filename extension by editing the value shown in the Extensions column, changing it to ColorMix. Now save your work, Build & Run, and save a color set, verifying that you now have no choice about what kind of file format to save it as.

Adding Color


Now the document stuff is all working as it should, but we’ve got a really boring application that doesn’t do anything interesting at all. Let’s make our app do what we promised in the beginning of this chapter: display a bunch of colors made by blending the two chosen colors using all of Core Graphics’ predefined blending modes.

To do this, we’re going to create a new class called ColorBlendView, which will be a direct subclass of NSView. One of the primary things you typically do in an NSView subclass is override the drawRect: mode to specify exactly what should be drawn. We’re going to do just that, and fill each view with a blended color. In order to do the blending, each instance of ColorBlendView needs to know which blending mode to use, and which two colors to blend. We’ll set up the blending mode for each ColorBlendView by hand, but the values for the two colors will be populated using Cocoa Bindings, so that as soon as the user changes one of the chosen colors, all of the ColorBlendView objects in our window will be instantly redrawn.

The ColorBlendView Class


Start by creating a new class in your project. In the New File assistant, choose Objective-C NSView subclass from the Cocoa section, name your new class ColorBlendView.m, and click the checkbox to also create the .h file.

Now edit ColorBlendView.h, adding the bold lines shown in the following:

This gives our class two NSColor objects, which will be populated by Cocoa Bindings, and a CGBlendMode, which will be set by the MyController class when it’s loading a nib file. Now switch over to ColorBlendView.m, where the first thing we’ll do is define the methods for the properties we’ve declared. Most of the properties we’ve declared previously in this book have their methods defined entirely with the @synthesize declaration, but here we have a bit of a special

Return Main Page Previous Page Next Page

®Online Book Reader