Dependency Injection with Storyboards

Hi everyone, great book. Lots of info to absorb!

I’m having a go at Dependency Injection as described in chapter 4. I’m using it with the MVVM pattern from Design Patterns by Tutorials (also awesome!). This is the method to load a custom storyboard view in its view controller (from Design Patterns).

MVVM

Question
Can we use dependency injection with a custom storyboard view? The problem I’m finding is the view controller’s view has to be loaded before the custom-view can be ‘injected.’ So we then have to return the view controller first, & then set the view controller’s view as! QuestionView after that. I can’t seem to make that work from outside the initial view controller. How might this method look in a Dependency container? And how might you ‘set it’ in the view controller?

Thanks! :smile:

@rcach Can you please help with this when you get a chance? Thank you - much appreciated! :]

You can use the loadView() function of the view controller to set your custom view. In your view controller you can set the following:

override func loadView() {
    self.view = questionView
}

@abnerabbey Thank you for sharing your solution - much appreciated!