Re: awakeFromNib multiple objects - all connected?
Re: awakeFromNib multiple objects - all connected?
- Subject: Re: awakeFromNib multiple objects - all connected?
- From: Roland King <email@hidden>
- Date: Fri, 11 Jul 2014 18:12:26 +0800
> On 11 Jul 2014, at 5:33 pm, Uli Kusterer <email@hidden> wrote:
>
> On 06 Jul 2014, at 08:54, Roland King <email@hidden> wrote:
>> After awakeFromNib all the outlets are connected except for those to subviews of the UIViewController's view, they remain nil until after viewDidLoad. Other top-level object outlets are connected, but not the view nor its subviews. I always felt that broke the awakeFromNib contract; the objects are in the NIB, the outlets are in the NIB, surely they should be connected at awakeFromNib.
>
> The contract is that all outlets loaded from the current NIB are connected. If you have cascading NIB loads, then you get an awakeFromNib call for each NIB (at least on the Mac, I don’t think I’ve had nested NIBs on iOS in a while). That means that e.g. a view controller in a NIB will get one awakeFromNib because the items in the NIB it owns have been loaded and connected, and then a second one when its parent NIB has finished loading and it’s been connected to the other items surrounding it.
>
> Depending on when the inner view controller is asked to provide its view, it may not have been loaded yet, so obviously there’s nothing to connect to yet. E.g. if the surrounding NIB calls -view on the view controller in -awakeFromNib, then of course it is until then that the remaining outlets are connected.
>
> I don’t see how that would break any contracts, it is logical. The alternative would be to somehow violate encapsulation and have an inner object inform the outer object that it has a NIB that needs loading as well, and then have it modify the load order and trigger awakeFromNib calls on objects actually instantiated by its owner and … it would be very messy, at the latest once you have a view controller inside the NIB of a view controller inside of the app’s main NIB or whatever.
ok so I wrote a test project for this. UIViewController subclass as file’s owner, a UIView subclass for its view, an extra test top-level object at the same level as the view, one button and one label as children of the view. I made the UIViewController subclass have outlets for the top-level object, the button and the label. Both the top-level object and the view had outlets for the button and the label and I hooked them all up. One NIB, all objects, 7 connected outlets.
Breakpointing in awakeFromNIB in the view controller and the top-level object showed that the outlets to the button and label were not hooked up, they were nil in both places. The VC was hooked up to the top-level object but that was the only connection which had been made.
That appears to violate that all outlets from the current NIB are connected.
.. which made me wonder, so I looked at the actual project directory and found .. there are TWO nibs in there. Seems IB evilly splits the viewcontroller piece from the view piece and puts them in their own nibs, along with some extra information to hook the view’s objects in one NIB into the VC’s objects in the other when the view loads (ie hooking the button and label to the top-level object in the first NIB).
That probably explains it. The original NIB just contains the VC and object, so awakeFromNIB only shows those two hooked up, the view load is a separate NIB load altogether.
That is evil, thanks for the thought about more than one NIB, it make me check to see if my one NIB really was .. one NIB.
_______________________________________________
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