What's the equivalent to loadNibNamed in Swift
Maybe the API has changed. The third parameter of loadNibNamed
is no longer options
, it's topLevelObjects
. The following code works for me:
var objects: NSArray?
NSBundle.mainBundle().loadNibNamed("MyView", owner: self, topLevelObjects: &objects)
NSBundle.mainBundle().loadNibNamed("Games-New", owner: nil, options: nil)[0]
Here is a UIView extension
public extension UIView {
public class func instantiateFromNib<T: UIView>(viewType: T.Type) -> T {
return NSBundle.mainBundle().loadNibNamed(String(describing: viewType), owner: nil, options: nil)?.first as! T
}
public class func instantiateFromNib() -> Self {
return instantiateFromNib(self)
}
}
Usage
let awesomeView = AwesomeView.instantiateFromNib()
OR
let awesomeView = UIView.instantiateFromNib(AwesomeView.self)