[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSWindowController, Nibs & Object ownership



----- Original Message -----
From: "Dan Wood" <djw_lists@xxxxxxxxxxx>
> This is one of the great mysteries of Cocoa, and I hope somebody
> can help clear this up....
>
> Dan
>


It is only one of the great mysteries of Cocoa because Apple was trying to
be a little bit too clever IMHO.
If you ever write your own document management classes, you will see that
nothing mysterious is happening.  All of the features of NSWindowController,
NSDocument, and NSDocumentController are (or could be) implemented using
existing delegate methods of NSWindow and NSApplication.

To answer your question: An NSDocument subclass "owns" one or more
NSWindowController instances.
An NSWindowController instance "owns" all of the top level objects in the
nib that it loaded.

All you have to do is release an instance of a subclass of NSDocument and it
will automatically release all of its window controllers and they in tern
release all of the top level objects loaded from a nib.

All you have to do is decide under what circumstances you are ready to
release an instance of a subclass of NSDocument and release it.  You may not
even need to do this because when a document closes it removes itself from
the document controller and the document controller releases the document.

If everything is configured correctly as it is by default, document closure
cleans up everything.  If that is not happening, check for places in your
code that retain documents or model objects.