Skip to content

briancroom/BlindsidedStoryboard

Repository files navigation

BlindsidedStoryboard

CI Status Version License Platform

Carthage compatible

Storyboards make dependency injection of view controllers challenging, because they insist on instantiating the view controllers internally. This restriction can be worked around by subclassing UIStoryboard and overriding the -instantiateViewControllerWithIdentifier: method to perform configuration work immediately following the instantiation. The same storyboard instance that is used to create the initial view controller will be used to instantiate further view controllers accessed via segues.

This library provides a BlindsidedStoryboard subclass of UIStoryboard which utilizes this technique, integrating with the Blindside DI framework. It includes a small sample app demonstrating how it can be used.

The BlindsidedStoryboard(CrossStoryboardSegues) category can be included to allow for seamless integration with Cross Storyboard Segues. Xcode 7's native storyboard references are also supported, and the same injector will continue to be used when switching storyboards.

Usage

When you create a storyboard instance to show a view controller, just do it like this:

id<BSInjector> injector = [Blindside injectorWithModule:[[MyBlindsideModule alloc] init]];
UIStoryboard *storyboard = [BlindsidedStoryboard storyboardWithName:@"Main" bundle:nil injector:injector];
UIViewController *viewController = [storyboard instantiateInitialViewController];
let injector = Blindside.injectorWithModule(MyBlindsideModule)
let storyboard = BlindsidedStoryboard("Main", bundle: nil, injector: injector)
let viewController = storyboard.instantiateInitialViewController()

BlindsidedStoryboard will ensure that viewController has its dependencies injected before it is returned to you. There are a few things to note:

  • Because the storyboard instantiates the view controller via -initWithCoder:, it is necessary to use +bsProperties to specify the class' dependencies.
  • Dependencies won't be available until after -awakeFromNib has been called.
  • You can use -bsAwakeFromPropertyInjection as a place to put work that needs to occur after dependencies have been injected.

Installation

BlindsidedStoryboard is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "BlindsidedStoryboard"

It can also be installed as a framework using Carthage if you are targeting iOS 8.0 or above. To get it this way, add the following line to your Cartfile:

github "briancroom/BlindsidedStoryboard"

Author

Brian Croom, brian.s.croom@gmail.com

License

BlindsidedStoryboard is available under the MIT license. See the LICENSE file for more info.