Reputation: 147
I'm creating popup custom UIView. I had created custom.xib, custom.swift. In my custom.xib, my owner's object refer to custom. I had implement init func in custom.swift and loadNib with the name "custom". I got infinity call from the init func until I got this warning and breakpoint at the super.init(coder: aDecoder)
.
warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
ViewController
let customView: Custom = Custom(frame: CGRectMake(100,100,200,200))
self.view.addSubview(customView)
Custom
var view: Custom!
override init(frame: CGRect) {
super.init(frame: frame)
loadNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadNib()
}
private func loadNib() -> Custom {
return NSBundle.mainBundle().loadNibNamed("Custom", owner: self, options: nil)[0] as! Custom
}
Upvotes: 1
Views: 443
Reputation: 1380
The reason you are getting this error is because your loadNib()
and init
methods are causing a recursion. imnosov's answer should solve your problem.
Upvotes: 0
Reputation: 856
Your loadNib()
method is useless in this case. It returns instance of Custom
view, but the result is never used by init
methods. You can declare your loadNib()
as a class method and remove init
in Custom
class:
class Custom: UIView {
class func loadNib() -> Custom {
return NSBundle.mainBundle().loadNibNamed("Custom", owner: self, options: nil)[0] as! Custom
}
}
Then you can use your loadNib()
method to instantiate Custom
view directly in ViewController
and change frame in this way:
let customView = Custom.loadNib()
customView.frame = CGRectMake(100,100,200,200)
self.view.addSubview(customView)
Upvotes: 1