Learn Objective-C on the Mac - Mark Dalrymple [112]
Open the Connections Inspector, and take some time to click on some of the menu items and see how they’re hooked up. You’ll find that many of the items in the File menu are connected to First Responder, calling actions whose names contain the word “document” (newDocument:, openDocument:, and so on). It turns out that several of these actions are implemented in the NSDocumentController class. This is a special class that has a single shared instance at runtime, for managing all the application’s open documents. You won’t see this shared instance, or even a proxy for it, in this nib file or anywhere else. The NSApplication creates this object during application launch, and is added to the responder chain so that it can handle these actions.
After you’ve taken a look around, you can close the MainMenu.xib for this application, and you’ll never need to open it again. Remember that the only real content in there is the menu, and most of the menu items are connected to actions on First Responder, which means they’ll find their way to the relevant window controller or the shared NSDocumentController when the time comes.
Now open MyDocument.xib and see what it has to offer. You’ll find an NSWindow ready for you to fill in some content, and if you open the Identity Inspector, you’ll see that the File’s Owner is an instance of MyDocument. Apart from that, it’s a blank slate, which you’ll make your mark on just a little later.
Defining the Model
Before we start building a GUI of any kind, let’s create the data model that will be used to represent a document. This is an extremely simple model, containing a single entity with two attributes. We’ll create just a single instance of that one entity in each document, but everything you learn about documents in this chapter applies equally well to large, complex data models.
Open the MyDocument.xcdatamodel. Create an entity and name it ColorSet. With the new entity still selected, create an attribute, name it color1, set its type to Transformable, and click to turn off the Optional checkbox. Then create a second attribute, name it color2, and configure the rest just like the first: the type is Transformable, and Optional should be turned off. These attributes represent the two colors that the user will choose for each document. Each of them contains an instance of NSColor, which is not one of the supported Core Data types, hence the use of Transformable as we described in Chapter 7.
Save the model, and you’re done with it. We told you it was an extremely simple model!
Setting Two Colors
Now it’s time to start putting the GUI together. Remember that this occurs in the document window, so switch back to MyDocument.xib in Interface Builder. The default window contains an NSTextField (“Your document contents here”), which you should delete before going on.
In previous Core Data examples, you learned how to drag entities from Xcode’s data modeling tool into Interface Builder. Now it’s time to learn another way that’s just as good. In the Library window, search for “core data,” and you’ll see the Core Data Entity appear in the results. This special object, when dragged into a window, will let you pick an Xcode project, data model file, and entity (see Figure 11-2).
Figure 11-2. This window lets you choose a data model and entity from among all open Xcode projects.
After clicking Next, it will run the same assistant that you’ve seen when creating the other Core Data apps. This time, choose the Single Item View option from the popup list,