FW-UP Multiple NSWindowcontroller for the same NSDocument
FW-UP Multiple NSWindowcontroller for the same NSDocument
- Subject: FW-UP Multiple NSWindowcontroller for the same NSDocument
- From: Andrea Perego <email@hidden>
- Date: Wed, 19 Dec 2001 13:55:34 +0100
First of all, many thanks to Raphael and Thomas for their answers and
code snippets: besides the fact that I found them very useful, it's
always interesting to learn different ways to skin a cat!
After some more debugging in the evening at home, I found that my
solution (see below) works, but I'd forgot something....
my subclass of NSWindowController was actually instantiated but, due
perhaps to its intrinsic "laziness" [BTW, I *love* this "laziness"
concept applied to computing. I believe that laziness, far from being
a sin, is the real engine that powers human progress: if it weren't
for it, we would still live in the stone age! ;) ] the corresponding
nib file was not loaded because the controller was not instructed to
show the window.
When a new document/controller/window set is created by opening a
file in a document based cocoa app, the NSDocumentController (or
subclass) sends
to your NSDocument's subclass "makeWindowControllers" and then
"showWindows". This last message makes you NSDocument's subclass issue
the "showWindow:(id) sender" action message to each (subclass of)
NSWindowController that has been instantiated. This last message
causes
the nib file to be loaded and the window to be displayed.
******* What follows is just a "from newbie to newbie" consideration:
cocoa experts may skip it altogether !! *************
While dealing with Cocoa as a newbie, one very easily concentrates on
her/his own code, forgetting the major role played by the run time
system and pre-fab
facilities as the doc-based-app environment. In my case, I was
pulling my hair trying to understand while my window controller
subclass did not, supposedly, perform its instantiation. To exit this
dead-end path, I put a break in the
"windowWillLoad" routine, which was called during the initialization
of the first window and not in my case. Looking at the "stack frame"
with the app stopped there, I realized the cause of my problem.
Best Regards
Andrea Perego
University of Florence - Phys. Dept.
Original message:
In my document-based project, a window displays a single part of a document
(a spectrum, while the document consists of an array of spectra). Therefore,
I'd like to allow the user to have more than a window open for the
same document, so that different spectra are displayed at the same
time. To achieve this goal, I've added a "New window" item to the
"Windows" menu, whose action message is received by the instance of
my NSDocument subclass (MySpectraDocument) that refers to the main
window. A new controller is created by means of
........
controller=
[[MySpectrumWController allocWithZone:[self zone]] initWithNumber:aNumb];
[self addWindowController:controller];
........
exactly as it is done in "makeWindowControllers" for the first
controller instance. [mySpectrumWController initWithNumber:..]
calls in turn
self = [super initWithWindowNibName: @"MySpectraDocument"];
..............
to load its window etc.
Since each window plays exactly the same role, but for displaying a different
spectrum from the collection, I guessed I might use the same nib
file for all of them.
In fact, while my application manages correctly a multi-window
environment where each window belongs to a different document,
whenever I try to create a further
controller/window for a document as outlined above, a new controller
is instantiated but neither "windowWillLoad" nor "windowDidLoad" is
called for it, and the corresponding window never appears.