3

I am an iOS developer trying to put together a little OSX app.

One thing I cannot figure out is how to properly inject dependencies in my viewControllers.

This is my setup:

NSWindowController
|
+- NSSplitViewController
   |
   +- NSViewController
   |
   +- NSViewController

Now I want to inject my data stack into all viewControllers before they start loading their tableViews and ask for some data.

1st attempt:
Coming from iOS I hook into [AppDelegat applicationDidFinishLaunching] to create my data stack and set it on the root view controller for passing along to its children.
Problem: It's too late – [NSViewController viewDidLoad] gets called before that (what?). My TableView starts asking for data before I had the chance to set it up.

Next thing I did was to do some logging to find out how the app setup cycle really works. I found out the live-cycle methods get called in that order:

[DKDSplitViewController viewDidLoad]
[DKDTableViewController viewDidLoad]
[DKDWindowController windowDidLoad]
[DKDWindowController awakeFromNib]
[AppDelegate applicationDidFinishLaunching:]

That seems quite weird to me, children being created before their parents...
Am I doing something wrong or are these the rules of nature on OSX?

So as it looks, I should do my setup in my SplitViewController. Somehow that feels wrong.

Ok, so how do I properly inject setup data into my view controllers on OSX?

de.
  • 7,068
  • 3
  • 40
  • 69
  • Don't know if it is the right way, but the way I do it is I untick the `Visible at launch` property of the windows in iB and do the injection in the init method of the view controllers, i.e. custom init methods with all the references needed. A little bit later I order the window to show which triggers the cascade of data requests from the views. Obviously with this way the view controllers are all instantiated from code, they are not inside the xib file – pco494 Feb 08 '16 at 02:13
  • After reading more examples and documentation, I get a feeling that dependencies are intended to be set up in the storyboard itself. Some explanation from an OSX pro would be very much appreciated! – de. Feb 08 '16 at 13:14
  • That may very well be the case, I would not know as I am not using storyboards – pco494 Feb 08 '16 at 17:51

0 Answers0