When are @IBOutlets set?

Howdy!

FYI This is mostly just a rephrasing of my question from the other week.

I have a superview which owns some subviews defined in a XIB. Until now, I have assumed that the @IBOutlet defined in the owner/superview are set during the call to UINib.instantiate(withOwner:options:). Is assumption this incorrect?

In practice at least, this assumption seems to mostly hold true. But now I have run into a case where only some of the IBOutlets remain unset even after I instantiate and add to the view hierarchy.

What gives?

Thanks, smkuehnhold

Answered by Frameworks Engineer in 725555022

Outlets are set during the instantiation process, however keep in mind that if you have weak outlets, and there is nothing holding a strong reference to them by the end of the instantiation process (such as, for views, being inserted into a view hierarchy that has a strong reference at its root) then those references may be nil'd out by the end of the instantiation process.

Accepted Answer

Outlets are set during the instantiation process, however keep in mind that if you have weak outlets, and there is nothing holding a strong reference to them by the end of the instantiation process (such as, for views, being inserted into a view hierarchy that has a strong reference at its root) then those references may be nil'd out by the end of the instantiation process.

That's very reassuring to know :). The way I have it now, these owned subviews are passed deep into the view hierarchy. Which means it is likely, as you alluded, I am dropping the reference somewhere.

Appreciate the clarification,

smkuehnhold

When are @IBOutlets set?
 
 
Q