I have code such as the following in my custom view:
Stream.of(a, b, c, d)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.ifPresent(s -> System.out.println("This was cumbersome: " + s));
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:
@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 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?
答案 0 :(得分:3)
看起来它正在被使用的NSBundle出现问题。我可以通过将A
更改为NSBundle.mainBundle()
来解决问题。
我从这篇博文中得到了这个想法:Create an IBDesignable UIView subclass with code from an XIB file in Xcode 6。