Skip to content

Instantly share code, notes, and snippets.

@jeamesbone
Last active October 19, 2016 04:53
Show Gist options
  • Save jeamesbone/65caf783a8427a03943cd31a22cfce12 to your computer and use it in GitHub Desktop.
Save jeamesbone/65caf783a8427a03943cd31a22cfce12 to your computer and use it in GitHub Desktop.
Storyboard Dependency Injection - Example
// First we need a scene for our ViewController
struct LandingScene: Scene {
// Non optional, safe references to our vc's dependencies
let client: APIClient
let account: Account
// Note the parameter type is the specific view controller we want to configure
// This takes advantage of the `associatedtype` we defined in our protocol
func configureViewController(_ viewController: LandingViewController) {
// Inject the dependencies
viewController.client = client
viewController.account = account
}
}
// Our good old ViewController
class LandingViewController {
var client: APIClient!
var account: Account!
}
let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil) // Get our storyboard
let scene = LandingScene(client: myClient, account: myAccount) // Create our scene
let vc = storyboard.instantiateViewController(with: scene) // Create our ViewController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment