Last active
October 19, 2016 04:53
-
-
Save jeamesbone/65caf783a8427a03943cd31a22cfce12 to your computer and use it in GitHub Desktop.
Storyboard Dependency Injection - Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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