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:
- Is it possible that because I am using a subclass of a subclass something messes up with the initialization? I am not overriding any initializers
awakeFromNib:
is not getting called for some reason- Maybe it doesn't connecting to subviews on subviews
Things I have tried:
- Matching
@IBOutlet
and storyboard item types exactly (instead ofUIView
) - Deleting property and outlet and re-added them
Figure A*
Figure B
*The obscured code in Figure A
is:
@IBOutlet private var annotationOptionsView: UIView!
@IBOutlet private var arrivingLeavingSwitch: UISegmentedControl!
Thank you.
For me, this was crashing because
containerView
was nil.Here is my code with Crash.
The missing thing was calling the
super.loadView()
. So adding it solved problem for me.Fixed Code:
Check to see if you have any missing or disconnected outlets.
Have you tried running Product > Clean. Solved a very similar problem for me.
2019, ONE POSSIBILITY FOR THIS HORRIBLE PROBLEM:
Say you have perhaps a container view that shows some sort of clock. So you have
class Clock: UIViewController
You actually use it in a number of places in the app.
On the main screen, on the details screen, on the edit screen.
You have a complicated snapchat-like modern app.
In fact,
Clock
may actually be loaded more than once at the same time somewhere on the same screen. (Maybe it's hidden in some cases.)You start working on one instance of
Clock
on one of your many storyboards.On that storyboard you add a label, NewLabel.
Naturally you add the outlet in code. Everything should work. All the other outlets work perfectly.
You have definitely linked the outlet.
But the app crashes with NewLabel as nil.
Xcode clearly tells you "you forgot to connect the outlet".
The reason is this .......... you have "NewLabel" on only one of the storyboard uses of Clock!
The crash is actually from >>> an other place <<<< you are using Clock!!!!
Xcode does not tell you the crash is from another place altogether, not from where you are working!
The crash is actually not from the place you are working - it's from another storyboard, where there is no "NewLabel" item on that storyboard!!!
Frustrating.
I see you use
ViewController
!? inViewController
class you must use-viewDidLoad
, not-awakeFromNib
,-awakeFromNib
use forUIView
classGot one more ...
If you have a custom class for a UITableViewCell but forget to specify Custom in the Style of the cell.