IBOutlet is nil, but it is connected in storyboard

2020-01-23 16:50发布

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 of UIView)
  • Deleting property and outlet and re-added them

Figure A

Figure A*

Figure B

Figure B

*The obscured code in Figure A is:

@IBOutlet private var annotationOptionsView: UIView!
@IBOutlet private var arrivingLeavingSwitch: UISegmentedControl!

Thank you.

30条回答
Animai°情兽
2楼-- · 2020-01-23 17:31

For me, this was crashing because containerView was nil.

Here is my code with Crash.

@IBOutlet private var containerView: UIView!  // Connected to Storyboard
override open func loadView() {
    containerView.addSubview(anotherView)
}

The missing thing was calling the super.loadView(). So adding it solved problem for me.

Fixed Code:

@IBOutlet private var containerView: UIView!
override open func loadView() {
    super.loadView()
    containerView.addSubview(anotherView)
}
查看更多
爷、活的狠高调
3楼-- · 2020-01-23 17:31

Check to see if you have any missing or disconnected outlets.

enter image description here

查看更多
男人必须洒脱
4楼-- · 2020-01-23 17:32

Have you tried running Product > Clean. Solved a very similar problem for me.

查看更多
我只想做你的唯一
5楼-- · 2020-01-23 17:32

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.

查看更多
时光不老,我们不散
6楼-- · 2020-01-23 17:32

I see you use ViewController!? in ViewController class you must use -viewDidLoad, not -awakeFromNib, -awakeFromNib use for UIView class

查看更多
小情绪 Triste *
7楼-- · 2020-01-23 17:33

Got one more ...

If you have a custom class for a UITableViewCell but forget to specify Custom in the Style of the cell.

查看更多
登录 后发表回答