Re: Collection of Cocoa & objc questions from a "newbie"
Re: Collection of Cocoa & objc questions from a "newbie"
- Subject: Re: Collection of Cocoa & objc questions from a "newbie"
- From: Art Isbell <email@hidden>
- Date: Tue, 17 Jul 2001 23:12:35 -1000
On Tuesday, July 17, 2001, at 06:29 PM, Bill Bumgarner wrote:
When a NIB file is loaded, any objects that appear as instances
within the NIB-- i.e. NOT the file's owner, but every other
object in the nib-- will first be instantiated (and have the
designated initializer called), then will have all the outlets
connected, then will have -awakeFromNib invoked...
Just to avoid further confusion (or maybe to create more
confusion :-) those objects that are actually archived in a nib
don't have their designated initializers invoked upon nib
loading but instead are sent initWithCoder:, the standard
unarchiving initializer. These archived objects are sent their
designated initializers when they are created in Interface
Builder.
However, just to confuse the issue further, there are some
"template" objects archived in nibs. These private objects
represent the real objects that developers think they are
creating in Interface Builder. During nib loading, these
templates allocate and initialize the real objects. So these
real objects do have their designated initializers invoked
during nib loading.
An example of one of these template classes is
NSWindowTemplate. When a window is added to a nib, the object
actually archived in the nib is a NSWindowTemplate instance that
stores all the window properties set in Interface Builder. When
a NSWindowTemplate is unarchived during nib loading, it
allocates and initializes a NSWindow object setting the
properties of this NSWindow accordingly. The NSWindowTemplate
then releases itself and is freed.
It's not usually necessary that the inner workings of
Interface Builder and nibs are fully understood, but I've been
tripped up by the assumption that no designated initializers
will be invoked during nib loading. This is true for most
objects, but not those represented by template objects. Others
may assume that designated initializers are invoked on all nib
objects, but that's not true either.
So how might this impact you? If you use an Interface
Builder CustomView to represent one of your custom views, an
instance of your custom view won't be archived in the nib, but
its designated initializer will be invoked during nib loading as
the CustomView template creates an instance of your custom view
class.
But if you create a custom Interface Builder palette that
includes your custom view and add that custom view to your nib,
its designated initializer would be invoked when the custom view
is added to the nib, not when the nib is loaded.
So one must consider these subtleties when implementing
designated initializers or when making assumptions about when
these designated initializers will be invoked.
I've been concentrating on WebObjects for several years, so
if nib loading has changed since OPENSTEP days and what I've
claimed is no longer true, someone please correct me. However,
I did just run a Cocoa example under gdb and set breakpoints on
initWithCoder: and designated initializer methods for various
classes. This test did not refute the above claims.
Art Isbell
Apple iServices Technical Support
http://www.apple.com/iservices/webobjectssupport/
+1-808-591-0836