xcode - How to instantiate NSViewController from a Framework on OS X (i.e. getting the correct bundle) -
how can instantiate view controller framework added os x app (this may same process ios, i'm unsure, last time looked frameworks not allowed on ios may have changed)?
here setup:
- private framework
myframework.framework
, - containing view controller
customviewcontroller
, - with xib
customviewcontroller.xib
.
what not work
customviewcontroller(nibname: "customviewcontroller", bundle:nil)
not work because resources not in app's main bundle (they in framework).
what work
to instantiate view controllers framework app using following code (it works!) iterates through loaded frameworks using nsbundle.allframeworks()
, picks out framework comparing bundleidentifier
,
func findmyframeworkbundle() -> nsbundle? { if let frameworkbundles = nsbundle.allframeworks() as? [nsbundle] { bundle in frameworkbundles { if let identifier = bundle.bundleidentifier { if identifier == "com.me.myframework" { return bundle } } } } return nil } let nibname = "customviewcontroller" let bundle = findmyframeworkbundle() let viewcontroller = customviewcontroller(nibname:nibname, bundle:bundle)
how improve?
this seems overly complex make me think must doing wrong. potential solutions:
- can change framework , provide custom initialiser instantiates view controller correct bundle?
for example,
class customviewcontroller : nsviewcontroller { init() { let bundle = // ... correct bundle when used embedded framework in app super.init(nibname: "customviewcontroller", bundle: bundle) } // ... other code }
- is there better way of providing correct bundle in app rather search through
nsbundle.allframeworks()
?
i may have misunderstood question, think standard practice use nsbundle
constructor takes identifier argument. in:
let bundle = nsbundle(identifier: "com.me.myframework")!