Re: awakeFromNib multiple objects - all connected?
Re: awakeFromNib multiple objects - all connected?
- Subject: Re: awakeFromNib multiple objects - all connected?
- From: Rob Petrovec <email@hidden>
- Date: Sat, 05 Jul 2014 18:10:18 -0700
> On Jul 4, 2014, at 9:13 PM, Graham Cox <email@hidden> wrote:
>
>
> On 5 Jul 2014, at 1:56 pm, Trygve Inda <email@hidden> wrote:
>
>> As long as A can call into B & C and know that B & C have their outlets
>> hooked up, that's fine.
>
> You can rely on all outlets being connected. What you can't rely on is the order in which each object's -awakeFromNib is called.
Actually, you can’t rely on all the outlets in the entire nib to be connected when the first awakeFromNib is called. This is an undocumented behavior, so it can change out from under you at any time. So you shouldn’t rely on this behavior just because it happens today, it could easily change in the some future release.
The only thing you can rely on, because it is documented, is that the outlets for ObjectA will be connected when ObjectA's -awakeFromNib is called, and the outlets for ObjectB will be connected when ObjectB's -awakeFromNib is called. There is no guarantee on the order of when their awakeFromNib’s will be called, so if you need to do something in one object that calls into another object from the nib (that is not a subview) then you should do it later in the loading sequence. Like when the nib’s owner has -awakeFromNib, windowDidLoad or -loadView called, or when the common-superview to ObjectA & ObjectB’s -awakeFromNib is called.
> On Jul 4, 2014, at 8:56 PM, Trygve Inda <email@hidden> wrote:
>
>>
>> On Jul 4, 2014, at 8:18 PM, Trygve Inda <email@hidden> wrote:
>>
>>> When an object in a nib receives awakeFromNib are all the outlets throughout
>>> the entire nib hooked up, or only those outlets in the object that is
>>> receiving awakeFromNib?
>>
>> All the outlets are hooked up. But not all the other objects in the nib have
>> run their -awakeFromNib methods yet, so you have to be cautious about calling
>> into other objects in the nib during your -awakeFromNib implementation.
>>
>> —Jens
>
> That's fine. My situation is I have a subclass of NSWindowController and
> several custom objects:
>
> Owner (MyWindowController)
>
> ControllerA
>
> ControllerB
> ControllerC
>
> These are all in the nib and I call init in such a way that Owner is passed
> a reference which it stores. When ControllerA gets an awakeFromNib it needs
> to call methods in ControllerB and ControllerC that require all the outlets
> to be hooked up.
>
> I need to ensure ControllerA runs first so I do this by having an
> awakeFromNib in ControllerA, but not in B or C. So that when ControllerA
> gets awakeFromNib, it can manage things.
>
> As long as A can call into B & C and know that B & C have their outlets
> hooked up, that's fine.
Having -awakeFromNib implemented in one object and not another does not determine the order. So you can’t rely on ControllerB & ControllerC having their outlets connected when ControllerA’s -awakeFromNib is called.
A better, and less fragile approach would be to override the Owner’s -awakeFromNib (or better yet windowDidLoad since it is an NSWindowController subclass) and have it instruct ControllerA to do whatever it is you wanted ControllerA to do in its awakeFromNib.
Good luck...
—Rob
_______________________________________________
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