I always find it a little bit annoying (for lack of a better word) to define a custom NSView
in a XIB
file.
I have switched to using a custom Storyboard (one per class) and a corresponding NSViewController
. When adding the view to my hierarchy I am simply using code that looks like this:
let viewController: CustomViewController = // Use my custom extensions to instantiate the NSViewController subclass.
self.view.addSubview(viewController.view)
Are there any drawbacks? Is it okay to use this approach or could there be any downsides (maybe a NSViewController
instance receives additional events or is somehow added to another hierarchy by default).
As a general rule, a view controller in a Storyboard is backed by code in its controller class.
.instantiateViewController(...)
is then used to load a view controller from a Storyboard and:
If you do have code backing that view controller, and you haven't added it as a child or assigned it to a class var/property, then the controller code will no longer exist as soon as viewController
goes out of scope. If you've connected any UI elements to that controller class, or you need to call any funcs in that class, you'll run into problems.
However, assuming you do not have any code associated with that VC, then it's really functionally equivalent to designing the view in a XIB and instantiating it with from the resulting Nib.