Skip to content

Instantly share code, notes, and snippets.

@rizumita
Last active March 9, 2016 15:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rizumita/d02427ddbd1d73d6e924 to your computer and use it in GitHub Desktop.
Save rizumita/d02427ddbd1d73d6e924 to your computer and use it in GitHub Desktop.
Using UIStoryboardSegue for purposes of injecting dependencies and passing data
class BundleSegue: ConfigurationSegue {
typealias Source = BundlesViewController
typealias Destination = BundleViewController
typealias First = BundlesViewController
typealias Second = BundleViewController
override func inject() {
destinationController.viewModel = resolver.resolve(BundleViewModelType.self)
}
override func propagate() {
second.id = first.selectedID
}
}
import UIKit
import Swinject
class ConfigurationSegue: UIStoryboardSegue, InjectionSegueType, PropagationSegueType {
typealias Source = UIViewController
typealias Destination = UIViewController
typealias First = UIViewController
typealias Second = UIViewController
func inject() {
}
func propagate() {
}
override func perform() {
self.inject()
self.propagate()
super.perform()
}
}
import UIKit
import Swinject
protocol InjectionSegueType {
typealias Source: UIViewController
typealias Destination: UIViewController
var sourceController: Source { get }
var destinationController: Destination { get }
var resolver: Resolvable { get }
func inject()
}
extension InjectionSegueType where Self: UIStoryboardSegue {
var sourceController: Source {
return sourceViewController as! Source
}
var destinationController: Destination {
return destinationViewController as! Destination
}
var resolver: Resolvable {
return assembler.resolver
}
}
import UIKit
protocol PropagationSegueType {
typealias First
typealias Second
var first: First { get }
var second: Second { get }
func propagate()
}
extension PropagationSegueType where Self: UIStoryboardSegue {
var first: First {
return sourceViewController as! First
}
var second: Second {
return destinationViewController as! Second
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment