Recall from the last chapter that Cocoa’s multiple-document architecture uses a subclass of the NSDocument class for loading and saving documents, and a subclass of the NSWindowController class for actually managing the document window itself. For simple applications, it may not be necessary to subclass both NSDocument and NSWindowController — you can put all of the necessary code in your subclass of the NSDocument class. However, if you plan to create several different windows for a single document, or if you want to have complex functionality embodied within your document windows, Apple recommends that you subclass NSWindowController. (For a more detailed discussion, we recommend that you read the Cocoa documentation pages for the NSDocument, NSWindowController, and NSDocumentController classes.)
In the next section, we will create a class called PaperController as
our subclass of the NSWindowController class. To teach MathDocument
about PaperController, we need to make two modifications to the
MathDocument.m
class file:
Recall that the MathDocument class is a subclass of NSDocument. If we
look at the code for MathDocument.m
that we
listed in the last chapter (or view it in PB), there is a comment in
the windowNibName
method that tells us to override
NSDocument’smakeWindowControllers
method whenever we subclass
NSWindowController. We’ll do just that in the next
step:
Override NSDocument’s defaultmakeWindowControllers method by adding the method shown
here in bold just before the @end
of the
MathDocument.m
file:
// Override the NSDocument makeWindowControllers // method to specify our own controller - (void)makeWindowControllers { PaperController *ctl = [ [PaperController alloc] initWithWindowNibName:[self windowNibName] ]; [ctl autorelease]; [self addWindowController:ctl]; } @end
This new method overrides NSWindowController’s makeWindowControllers method. It creates an instance of PaperController, autoreleases the instance, and finally adds the instance to the list of managed window controllers using addWindowController: .
There is another automatically generated comment that says we should remove the implementation of windowNibName when overriding the makeWindowControllers method. We have not found this necessary using Mac OS X Version 10.1, but it may be necessary in the future.
You may be confused about why we autorelease the PaperController instance — after all, don’t we want to use this object later? Well, recall from Chapter 4 that the autorelease message doesn’t immediately release an object. Instead, it decrements the object’s reference count. If no other part of our program increments the object’s reference count by the time that the next event is requested from the user, the object will automatically be freed. In this case, however, the PaperController object will automatically be retained by the addWindowController: method call.
We need to autorelease the PaperController object because we alloc-ed it. But we don’t want to send the object a full-blown release message, because we don’t want it to be freed. It is for this purpose that the autorelease method was invented.
It’s finally time to create the PaperController class that we’ve been discussing. Most of our “controlling” code will be placed in this class.
18.219.220.22