-
Notifications
You must be signed in to change notification settings - Fork 72
Storyboards integration
Ilya Puchka edited this page Apr 18, 2016
·
8 revisions
If you use storyboards and segues to instantiate view controller then you don't have an option of constructor injection for controller, because their instances will be created by UIKit. But you can still use property injection. To let DependencyContainer
intercept the process of creating view controller you need to follow few steps:
- add Dip-UI extension to your project
pod "Dip-UI"
or github "AliSoftware/Dip-UI"
- in Interface Builder set a user-defined attribute for view controller where you want to inject properties to with name
dipTag
and some unique value
- when you configure you container you need to register view controller using the tag that you used for
dipTag
attribute in Interface Builder. ProvideresolveDependencies
block that will perform property injection (you can also use auto-injection)
container.register(tag: "myVC") { MyViewController() }
.resolveDependencies { container, controller in ... }
- set the container as the one to be used for components created from storyboard (you can use it not only for view controller but for any
NSObject
that you put on storyboard)
DependencyContainer.uiContainer = container
- make your view controller to conform to
StoryboardInstantiatable
. You don't need add anything to conform to this protocol, because Dip-UI already provides the default implementation forNSObject
(check Dip-UI readme for mo info aboutStoryboardInstantiatable
).
extension MyViewController: StoryboardInstantiatable { }