Created
July 17, 2016 09:52
-
-
Save shiweifu/50f1fe54a0385ffd56cc6e4d482360a5 to your computer and use it in GitHub Desktop.
load NSView from nib file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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