mbogh / NibDesignable

Elegant way of enabling IBDesignable on your nib-based views
MIT License
342 stars 74 forks source link

custom view cannot be initialized #2

Closed derekshi closed 9 years ago

derekshi commented 9 years ago

I inherited my custom view from NibDesignable and it crashes on following lines: super.init(coder: aDecoder)

mbogh commented 9 years ago

Could you reveal a bit more information? Maybe show some code?

Possible problems:

derekshi commented 9 years ago

thank you. I resolved the issue as you guided. But NibDesignable seems not to be working on IB when I drag a new UIView into viewcontroller and set this UIView to my custom view class. On the identity inspector tab shows 'Designables Build Failed'. When I click on 'Show' button to the right, it just shows nothing. Not sure how to debug this. Any suggestions?

mbogh commented 9 years ago

Does it work when you run your app?

Do you do any work in init(frame: CGRect) or init(coder aDecoder: NSCoder) ? Remember that IB will call init(frame: CGRect) and that init(coder aDecoder: NSCoder) will be called on runtime.

Also check your code in prepareForInterfaceBuilder(), a crash there will also give the error.

derekshi commented 9 years ago

Ok, here is my custom view class:

class RestaurantSimpleView: NibDesignable {

@IBOutlet weak var resName: UILabel!
@IBOutlet weak var address: UILabel!
@IBOutlet weak var resPhone: UILabel!

let _nibName = "RestaurantSimpleView"

@IBInspectable var nameRes: String? {
    didSet {
        resName.text = nameRes
    }
}

override func nibName() -> String {
    return _nibName
}

var restaurant: Restaurant? {
    didSet {
        resName.text = restaurant!.name
        address.text = "\(restaurant!.street)\r\n\(restaurant!.city), \(restaurant!.state) \(restaurant!.zipcode)"
        resPhone.text = restaurant!.phone
    }
}

}

I don't necessary need the 'nameRes' variable. I just want to test it out. But doesn't seem to work.

mbogh commented 9 years ago

Hi @derekshi I tried recreating your view and everything worked fine in Xcode 6.0.1

My swift and xib file: https://www.dropbox.com/sh/7wqs1xvfjs4v33d/AACZKRiN_FeKHd_OMgi6L02ka?dl=0

mbogh commented 9 years ago

I'm closing this due to inactivity.