Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent to loadNibNamed in Swift

Tags:

swift

I am converting my Objective-C code to Swift and was wondering what is the swift equivalent to this code

[[[NSBundle mainBundle] loadNibNamed:@"Games-New" owner:nil  
options:nil] firstObject]; 

Thanks.

like image 515
Michael Rivers Avatar asked Jun 10 '14 05:06

Michael Rivers


8 Answers

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)
like image 187
Tyler Liu Avatar answered Oct 03 '22 21:10

Tyler Liu


NSBundle.mainBundle().loadNibNamed("Games-New", owner: nil, options: nil)[0]
like image 20
Dash Avatar answered Oct 03 '22 23:10

Dash


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)
like image 25
aryaxt Avatar answered Oct 03 '22 23:10

aryaxt


Swift 3


name
The name of the nib file, which need not include the .nib extension.

owner
The object to assign as the nib’s File's Owner object.

options
A dictionary containing the options to use when opening the nib file.

first
if you do not define first then grabing all view .. so you need to grab one view inside that set frist .

Bundle.main.loadNibNamed("yourUIView", owner: self, options: nil)?.first as! yourUIView
like image 45
Nazmul Hasan Avatar answered Oct 03 '22 21:10

Nazmul Hasan


Use this to load your nib.

let myView = NSBundle.mainBundle().loadNibNamed("MyView", owner: nil, options: nil)[0] as UIView

Then you can add it to your view as you normally would.

view.addSubview(myView)
like image 35
Kyle Clegg Avatar answered Oct 03 '22 21:10

Kyle Clegg


For Swift 3:

Bundle.main.loadNibNamed("Games-New", owner: nil, options: nil)?[0]

FYI: NS is (not entirely) removed in Swift 3

like image 26
Yannick Winters Avatar answered Oct 03 '22 23:10

Yannick Winters


In the docs, it looks like it is different for macOS and iOS.

For macOS and Swift 4, the following worked for me:

func loadFromNib(_ nibName: String) -> NSView? {
    var topLevelObjects: NSArray?
    if Bundle.main.loadNibNamed(NSNib.Name(rawValue: nibName), owner: self, topLevelObjects: &topLevelObjects) {
        return topLevelObjects?.first(where: { $0 is NSView } ) as? NSView
    } else {
        return nil
    }
}
like image 36
Ryan H Avatar answered Oct 03 '22 22:10

Ryan H


in Swift 4, NSBundle.mainBundle changed to Bundle.main.loadNibNamed

Bundle.main.loadNibNamed("MyDenizbankLoading", owner: nil, options: nil)![0]
like image 30
Emre Gürses Avatar answered Oct 03 '22 23:10

Emre Gürses