Reputation: 91911
I have code such as the following in my custom view:
@IBDesignable
class ABCMyView: UIView {
// (IBOutlets and other code omitted)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
private func commonInit() {
let nib = UINib(nibName: "ABCMyViewContents", bundle: NSBundle.mainBundle())
let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
addSubview(view)
}
}
When attempting to view ABCMyView in Interface Builder, it crashes. Attempting to debug the crash reveals that it crashes on the line that attempts to instantiate the nib with an owner. There isn't an error message, just a stack trace that ends up in assembly code (libobjc.A.dylib`objc_exception_throw:).
Here's some things I had tried in the console on the exception breakpoint:
(lldb) po nib
<UINib: 0x78ef8f20>
(lldb) po nib.instantiateWithOwner(self, options: nil)
error: Execution was interrupted, reason: breakpoint 1.1.
The process has been returned to the state before expression evaluation.
When I run the app, everything works perfectly, it's only when attempting to see the UIView in Interface Builder that fails.
How can I load a Nib file from an IBDesignable?
Upvotes: 4
Views: 1294
Reputation: 91911
It looks like it's a problem with the NSBundle that is being used. I was able to resolve the problem by changing NSBundle.mainBundle()
to NSBundle(forClass: self.dynamicType)
.
I got the idea from this blog post: Create an IBDesignable UIView subclass with code from an XIB file in Xcode 6.
Upvotes: 3