8

I am trying to embed a UIView I've prepared, which is located in a xib file, to a storyboard.

What I've done so far is:

class TestUIView : UIView {
    @IBOutlet weak private var firstButton: UIButton!
    @IBOutlet weak private var secondButton: UIButton!

    // MARK - Lifetime

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        let view = NSBundle.mainBundle().loadNibNamed("TestUIView", owner: self, options: nil).first as! TestUIView

        self.addSubview(view)
    }
}

But for some reason I get bad access memory exception. From looking into stack trace I see a whole bunch of calls to initWithCoder https://i.stack.imgur.com/iH6Am.png I'm not sure why NSBundle.mainBundle().loadNibNamed causes this, any ideas?

1
  • Presumably the nib file contains an instance of a TestUIView, so whenloadNibNames loads the nib, it will instantiate a TestUIView instance by called init(coder) which loads the nib and so on until the recursion causes the stack to explode. why are you loading the nib within init(coder)?
    – Paulw11
    Nov 29, 2015 at 11:58

3 Answers 3

15

Turn's out what I have done wrong is how I prepared the .xib file, I've set the View itself, instead of the file owner to TestUIView class. After changing the file owner(and reseting the constraints), everything worked fine.

1
  • Mark your answer as resolved so it can help other people :) Nov 30, 2015 at 13:41
8

For future seekers....

Brief : The recursive call of required init?(coder aDecoder: NSCoder) occurs when you accidentally set your custom view class in identity inspector of your ContentView.

When the compiler tries to load your Nib file(the owner view) it first needs to initiate your ContentView (see the following picture) and if you have mistakenly set the custom class for your ContentView or any of its subviews, it goes through another Nib loading process and stuck in a recursive infinite loop.

To do it the right way, you should only set your custom view class for the view in Placeholder’s part. Please see 1 and 2 in the following picture.

In this picture I have defined a custom class named CardView.

enter image description here

1

You just defined the way to build a TestUIView that contains a TestUIView that contains a TestUIView that contains a TestUIView...

Your don't have to call

let view = NSBundle.mainBundle().loadNibNamed("TestUIView", owner: self, options: nil).first as! TestUIView

from

init?(coder aDecoder: NSCoder)

This way you are creating an infinite loop because loadNibNamed will automatically call the init?(coder aDecoder: NSCoder).

Just remove these lines

let view = NSBundle.mainBundle().loadNibNamed("TestUIView", owner: self, options: nil).first as! TestUIView
self.addSubview(view)
2
  • 1
    But still, how do I connect the nib to the class? Removing those classes, leaves my outlets unset.
    – Gil Polak
    Nov 29, 2015 at 12:07
  • btw, this is where I got this idea from: iphonedev.tv/blog/2014/12/15/…
    – Gil Polak
    Nov 29, 2015 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.