Re: Multiple invocations of awakeFromNib on a single instance?
Re: Multiple invocations of awakeFromNib on a single instance?
- Subject: Re: Multiple invocations of awakeFromNib on a single instance?
- From: David Rehring <email@hidden>
- Date: Thu, 24 Jan 2002 15:18:14 -0800
On 1/24/02 12:37 PM, Evan Coyne Maloney at email@hidden wrote:
>
From what I understand, awakeFromNib is ONLY called on an instance when
>
that instance is decoded from the NIB; I have NOT seen anything indicating
>
that awakeFromNib will ALSO be called on an instance that's acting as a
>
File's Owner when loading another NIB.
>
>
Have I found a bug? Or is awakeFromNib supposed to be called in this
>
fashion?
>
>
If this IS the correct behavior of awakeFromNib, is there another message
>
I can handle if I want to do one-time initialization when an object is
>
decoded from the NIB? (I am currently allocating NSMutableArrays within my
>
awakeFromNib, and this is causing a problem, since my arrays are getting
>
overwritten when the second awakeFromNib is sent. Obviously, I can create
>
a flag along the lines of "alreadyAllocatedArrays" to work around this, but
>
I'd rather understand what's really going on before I hack around the
>
problem.)
>
>
Thanks in advance for your insight!
I believe this is the correct behaviour [at least, it's the behavior that's
occurred since NeXTStep 3.1...].
Personally, I track which 'instance' has occurred by looking at which
outlets are initialized.
Ie. For a 'non-document' application, I'll create a custom class and
instantiate it in the main nib and make it the delegate of the application,
so when the application launches, it creates my custom object and makes it
the delegate of NSApp.
Then in awakeFromNib, my 'window' outlet will be nil, so I'll load a second
nib whose controller is my custom object. This results in a second call to
awakeFromNib, but this second time, my 'window' outlet is non-nil, so I
handle setting up the window at this time.
But, I only do this kind of thing for 'singletons'. If there would be
multiple instances of the window, I would instead explicitly create my
custom object, which then loads the correct nib file [which results in it's
awakeFromNib method only being called once].
Later,
--
David Rehring Psychos do not explode when light hits
Senior Software Engineer them, no matter how crazy they are...
Atimi Software, Inc.
www.atimi.com And totally insane guy!