1

I am trying to load Custom UIView from a nib, but it shows error on

super.init(coder: aDecoder) as EXC_BAD_ACCESS

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

    // Setup view from .xib file
    self.xibSetup()
}

When I tried to print aDecoder then it prints as

error: Trying to put the stack in unreadable memory at: 0x7fff5ba3af00.

My func xibSetup() is as below:

fun xibSetup() {
let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "Dialog", bundle: bundle)

    // Assumes UIView is top level and only object in CustomView.xib file
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

// use bounds not frame or it'll be offset
    view.frame = bounds

    // Make the view stretch with containing view
    view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]

    // Adding custom subview on top of our view (over any custom drawing > see note below)
    addSubview(view) }
Swapnil Dhotre
  • 375
  • 4
  • 21
  • What you had done in xibSetup? you want to load nib in Viewcontroller right? – Jigar Tarsariya Jun 20 '16 at 07:03
  • @Jigar I have edited my question now you can see it. – Swapnil Dhotre Jun 20 '16 at 08:45
  • @SwapnilDhotre What is `super` in this context? Init doesn't seem to be a good place to do UI manipulation like altering frames and adding subviews. – lawicko Jun 20 '16 at 09:24
  • @lawicko Here super will be UIView. Actually this is working on another controller but when tried same on another it shows issue. – Swapnil Dhotre Jun 20 '16 at 11:58
  • @SwapnilDhotre Something is wrong here, somewhere else in your code you instantiate this view from xib, this is when the `initWithCoder:` is called, then in your `initWithCoder:` you load the nib again and add a view to the superview, are you sure this is what you want to do? I think you should show more code so we know in what circumstances is the original `initWithCoder:` called. – lawicko Jun 20 '16 at 12:22
  • @lawicko I have followed this which works fine on one controller but same is not working on another http://stackoverflow.com/a/31957247/4869244 – Swapnil Dhotre Jun 20 '16 at 12:43

0 Answers0