Re: Why getting two awakeFromNib messages??
Re: Why getting two awakeFromNib messages??
- Subject: Re: Why getting two awakeFromNib messages??
- From: Andy Lee <email@hidden>
- Date: Sun, 25 Oct 2009 23:53:52 -0400
On Oct 25, 2009, at 10:30 PM, DairyKnight wrote:
Thanks for the answer. Do you know why the file's owner gets
awakFromNib
call as well? Is it creating a new file's owner object?
Remember the purpose of a nib is to instantiate objects, set their
properties, and set connections between them. The answer to your
question lies in reviewing how this is done.
When a nib is loaded, the following steps are performed in a well-
defined order:
(1) The objects you created in the nib are instantiated and initted,
and their non-outlet properties are assigned the values you specified
when you created the nib. (The File's Owner already exists at the
time loadNibNamed:owner: was called, so of course it does not need to
be instantiated, nor is it initted by the loading of the nib.)
(2) The objects' outlets are set as indicated by the connections you
made when you created the nib. These outlet connections include the
connections you made to and from the File's Owner.
(3) awakeFromNib is sent to every object in the nib, *including* the
File's Owner. Because step (2) has already been done for all objects
in the nib, awakeFromNib can now send messages to outlet objects --
this is the reason for having awakeFromNib in the first place.
If you load a second nib using the same File's Owner as for the first
nib, then all the above three steps are performed again for the second
nib, including step (3). That is where you are getting your second
call to awakeFromNib.
The "Nib Files" chapter goes into much more detail: <http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html
>. But really, all the above is in the doc for awakeFromNib. I've
said this before: if something about a method is unclear, make sure
you've read the doc for that method. It sounds awfully obvious, but I
don't mean to be snotty; I mean it as a sincere reminder (and
something I need to remember as well).
I tried to NSLog(@"%@", self); in maincontroller's awakefromnib, but
the
second call to that raised an exception.
Sounds like there's something about your awakeFromNib method that
assumes it only gets called once. One kludgy thing you could do is
pick an outlet that is set by the second nib and implement
awakeFromNib like this:
- (void)awakeFromNib
{
if (outletThatIsSetBySecondNib != nil)
{
// Do stuff you wanted to do only after loading the second nib.
}
else
{
// Do stuff you wanted to do only after loading the first nib.
}
}
This is fragile and error-prone, and I have a feeling a better
solution has been offered when this question has come up before, but I
can't think of it offhand. If you need more specific help, you'll
have to show your code for awakeFromNib.
--Andy
In the code I was trying to pass the second controller as a pointer
to the
maincontroller so its methods could be called. Is there a better way
in
Cocoa to do this?
Regards,
DairyKnight
On Sun, Oct 25, 2009 at 5:21 PM, Volker in Lists <email@hidden
>wrote:
Hi,
you load the second nib from the main controller i guess. Thereby
you set
the instance of your main ontroller as owner, thus it gets the
awakeFromNib
call. Set your second controller as owner or nil (if you have an
instance of
your second class in your NIB as file owner (!)).
Cheers,
Volker
Am 25.10.2009 um 09:55 schrieb DairyKnight:
Hi all,
I'm trying to use two nib files in my program. In my main controller
class,
I load the second nib with:
[NSBundle loadNibNamed:"second" owner:self];
But then, not only does the second controller in the second nib file
recevie
the awakeFromNib message, but also does the main controller
class. Anyone knows why? Or Cocoa just broadcasts this message to
every
class?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden