Online Book Reader

Home Category

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

By Root 973 0
windows beside one another, to be able to compare them? Most people are familiar with these possibilities from using almost any modern application, where multiple documents can be open simultaneously, and actions in one don’t affect the others.

As it turns out, the good folks at Apple thought of this years ago, and built document support right into Cocoa, centered around the NSDocument and NSDocumentController classes. Cocoa’s document architecture gives you access to a whole lot of infrastructure that you’d otherwise have to build yourself, such as managing the window and its title bar, dealing with the open panels and save panels, and more. If you’re using Core Data in your application, it will even take care of the actual opening and saving all on its own, so your application code never even has to touch a document file.

In this chapter, you’ll learn the basics of how to create a document-based Cocoa application, including the use of Core Data, by creating an application called ColorMix. ColorMix lets the user choose two colors using the standard system color panel, and then presents a grid of color swatches demonstrating different methods of blending the two colors together. Each set of two chosen colors (and its 15 blended colors) can be saved in a document, which is just like any other file and can be saved anywhere you like, and then reopened later on. Along with learning about how Cocoa does documents, you’ll also build some simple view classes for drawing colored rectangles, and interact with Core Data in your code. Figure 11-1 shows what the ColorMix application will look like when we’re all done.

Figure 11-1. The completed ColorMix application

If you’ve used graphics software such as Photoshop or Gimp, you’re probably familiar with some of these blend modes. Each of them uses a particular formula that takes the red, green, and blue components of each of the two colors and produces an output color. We won’t describe the blend modes here, but you can find detailed descriptions and examples of them at http://en.wikipedia.org/wiki/Blend_modes and elsewhere. What we will do here is show you how to build an application that gives the user a hands-on way to explore all of these modes, two chosen colors at a time.

The blend modes we’ll be using are all built into Core Graphics, an important part of Cocoa’s graphics architecture that we’ll talk more about in Chapter 13. Using this built-in functionality lets you skip doing any sort of color computation on your own; all you do is call Core Graphics functions to set blend modes, and draw rectangles.

Creating the ColorMix Application


Start by creating a new application in Xcode. The new application will use both NSDocument architecture and Core Data, so you’ll want to pick the appropriate template when creating the application. If you’re running Xcode 3.1 on Leopard, this means choosing the Core Data Document-based Application template in the Mac OS X / Application section of the New Project assistant; in Snow Leopard, it means choosing the Cocoa Application template, then making sure that the checkboxes for creating a document-based application and for using Core Data are both checked. Name the new application ColorMix. As we’ve done before, open the Inspector panel for the project, find the build setting for Objective-C Garbage Collection, and set it to Required.

The ColorMix application will have a few things you haven’t seen created for you before. In particular, it has a class called MyDocument (a subclass of NSPersistentDocument, which is itself a subclass of NSDocument, both of which we’ll explain a little later), and a matching MyDocument.xib resource. These are the two key elements that you’ll extend to define the behavior and appearance of your documents. Apart from that, you’ll also see a model file where we’ll set up the Core Data entity and attribute information for this app.

Examining the Default Nib Files


Before we get to work on creating anything, open the MainMenu.xib file in Interface Builder. You might notice something different

Return Main Page Previous Page Next Page

®Online Book Reader