Skip to content

Instantly share code, notes, and snippets.

@shiweifu
Created July 17, 2016 09:52
Show Gist options
  • Save shiweifu/50f1fe54a0385ffd56cc6e4d482360a5 to your computer and use it in GitHub Desktop.
Save shiweifu/50f1fe54a0385ffd56cc6e4d482360a5 to your computer and use it in GitHub Desktop.
load NSView from nib file
extension NSView {
class func loadWithNibNamed(nibNamed: String, loadClass: AnyObject, tag: Int = 0) -> AnyObject? {
var topLevelObjects: NSArray?
let nib = NSNib(nibNamed: nibNamed, bundle: NSBundle.mainBundle())!
nib.instantiateWithOwner(nil, topLevelObjects: &topLevelObjects)
for object: AnyObject in topLevelObjects! {
if(object.isKindOfClass(loadClass.classForCoder)) {
if(tag != 0) {
let control = object as! NSControl
if(control.tag == tag) {
return object
}
}
else {
return object
}
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment