Using Swift 1.1 and Xcode 6.2.
I have a UIStoryboard containing a singular, custom UIViewController
subclass. On it, I have an @IBOutlet
connection of type UIView
from that controller to a UIView
subclass on the storyboard. I also have similar outlets for subviews of that view. See figure A.
But at run time, these properties are nil (Figure B). Even though I have assured I've connected the outlets in Interface Builder.
Thoughts:
awakeFromNib:
is not getting called for some reasonThings I have tried:
@IBOutlet
and storyboard item types exactly (instead of UIView
)Figure A*
Figure B
*The obscured code in Figure A
is:
@IBOutlet private var annotationOptionsView: UIView!
@IBOutlet private var arrivingLeavingSwitch: UISegmentedControl!
Thank you.
Typically this happens because your view controller hasn't loaded its view hierarchy yet. A view controller only loads its view hierarchy when something sends it the view
message. The system does this when it is time to actually put the view hierarchy on the screen, which happens after things like prepareForSegue:sender:
and viewWillAppear:
have returned.
Since your VC hasn't loaded its view hierarchy yet, your outlets are still nil.
You could force the VC to load its view hierarchy by saying _ = self.view
.