startupthekid startupthekid - 11 days ago 4
iOS Question

UIViewController subviews existance invariant

At what point in the

UIViewController
lifecycle is the
subview
property of
self.view
guaranteed to be populated with all the correct views? Note: I don't care about if they're laid out or not, just that they exist in the subview array. WWDC videos say that
loadView
,
viewDidLoad
, and
init
all don't come with that guarantee but
viewWillLayoutSubviews
is also late in the game.

The task I'm trying to perform in this instance is localization. In a common view controller class, I want to loop through all the subviews, see if they have a custom attribute set that identifies what localized string key is attached to that view, and then recurse through all that views subviews until the bottom of the view hierarchy is reached. Layout isn't important in this instance, just that the subviews are populated in the view controller.

Answer

At what point in the UIViewController lifecycle is the subview property of self.view guaranteed to be populated with all the correct views

The earliest point implemented by most apps is viewDidLoad. At that point you are guaranteed that self.view exists along with all the subviews from the storyboard, and that any outlets hooked up to this view controller from the storyboard have been populated.

I don't care about if they're laid out or not, just that they exist in the subview array

Exactly so. self.view and its nib-loaded subviews exist at this point, but their layout has not yet taken place and their frame is not necessarily correct. You don't care, so viewDidLoad is fine for your purposes.

Comments