Online Book Reader

Home Category

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

By Root 972 0
click on through the next window where you can choose which attributes should appear, and that part’s done. Your nib file now contains a properly configured NSObjectController, and the window contained in the nib file now has a very minimal GUI, featuring a text field for each color.

The Simplest of GUIs


That auto-generated GUI is not only minimal, it’s also inappropriate: our attributes are meant to be NSColor instances, not strings or anything else that can fit into a text field, so the first thing to do is delete those text fields. We also have everything held inside a box labeled Color Set, which we really don’t need because that’s the only entity this app has. Select the box by clicking its title, then choose Layout➤Unembed Objects from the menu. To underline the fact that the two colors will eventually mixed together, add the word “Mix” to the two remaining labels’ titles, ending up with “Mix Color 1” and “Mix Color 2”. Let’s face it: that whole GUI is pretty useless for our purposes. We really only dragged in the entity from the Library window because it gives us a nice, properly configured NSObjectController to work with. Speaking of which, select that controller object in the nib window and, keeping in line with what we’ve done with these controllers in the past, rename it to colorset.

Now find an NSColorWell in the Library window and drag it to the document window you’re building. Once it’s in place, duplicate it with ⌘D, and then rearrange the labels and color wells to look something like Figure 11-3.

Figure 11-3. The bare minimum GUI for selecting two colors

Now select the color well on the left, open the Bindings Inspector, and configure its Value binding to use the colorset controller with the selection controller key and the color1 model key path. Select the color well on the right, and configure its Value binding to use the colorset controller with the selection controller key, this time with the color2 model key path. Save your work, switch back to Xcode, and click Build & Run. Our app launches, and a new, empty “Untitled” document window appears, but the color wells are unclickable! In our haste to get something onscreen, we neglected to create something pretty important: a model object!

Creating a Default ColorSet


What we need to do is write some code that will insert a new ColorSet object into the object controller whenever a new document is created. In Xcode, open the MyDocument.h file. Here we’re going to add two instance variables. One is an outlet called objectController for pointing to the NSObjectController that the assistant put into our nib file a page or two ago. The other is a BOOL called isNew that we’ll use to keep track of whether each document has just been created or was loaded from a file. Here are the changes for the .h file:

Now switch to the .m file. This file contains some ready-made methods, with comments showing where you can extend their behaviors. We’re going to implement a bit of functionality in the init and windowControllerDidLoad: methods, as well as implementing the initWithType:error: method, which we’ll explain in a bit. The predefined methods in here all come from NSDocument, which handles most of the document-related functionality for us. MyDocument’s direct superclass, however, is NSPersistentDocument, which implements additional functionality for storing documents as Core Data storage back-ends. Here you see the lines you need to add:

The bits of bold code are executed when a new document is being created. The first, init, is called on every instance of MyDocument; here, we simply set isNew to NO. The second, initWithType:error:, is only called when a new document is being created from scratch. This is where we’d ideally want to initialize our model objects, but all access to the model objects is mediated through the NSObjectController, which is in the nib file, which at this point hasn’t been loaded yet. So we just set the isNew flag to True, and move along. Finally, windowControllerDidLoadNib: is called in MyDocument after its

Return Main Page Previous Page Next Page

®Online Book Reader