Githawk: Refactor VCs from Storyboard with a "create" method w/ all required params

Created on 20 Nov 2017  路  3Comments  路  Source: GitHawkApp/GitHawk

Instead of doing this:

https://github.com/rnystrom/GitHawk/blob/master/Classes/Issues/Managing/IssueManagingSectionController.swift#L64-L75

We should require VCs initialized from a Storyboard to have their own, static func create(...) that take in all params that you would see in the configure(...) method. This get us:

  • Single "initializer"
  • Dependency injection

Is there a way we could do this in a generic way so there's only one way to initialize VCs from Storyboards?

馃幆 project management

Most helpful comment

Love it. I'd like the create() method to take in some sort of params to config the view controller.

Would this work?

protocol SomeName: class {
  associatedtype ConfigureType
  var storyboardName: String { get }
  func configure(injected: ConfigureType)
}

extension SomeName where Self: UIViewController {

  static func create(injected: ConfigureType) -> Self? {
    let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
    let controller = storyboard.instantiateInitialViewController() as? Self
    controller?.configure(injected: injected)
    return controller
  }

}

class SomeVC: UIViewController, SomeName {
  struct Params {
    let title: String
  }
  typealias ConfigureType = Params

  var storyboardName: String { return "Milestones" }

  func configure(injected: Params) {
    self.title = injected.title
  }
}

// Anywhere else
let params = SomeVC.Params(title: "My Title")
let controller = SomeVC.create(injected: params)

All 3 comments

Maybe something like this: (Haven't a clue if this would even work, just writing yano)

protocol SomeName: class {
    var storyboardName: String { get }
}

extension SomeName where Self: UIViewController {

    static func create() -> Self? {
        let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
        return storyboard.instantiateInitialViewController() as? Self
    }

}

class SomeVC: UIViewController, SomeName {
    var storyboardName: String { return "Milestones" }
}

// Anywhere else
SomeVC.create() = Optional(SomeVC)

Love it. I'd like the create() method to take in some sort of params to config the view controller.

Would this work?

protocol SomeName: class {
  associatedtype ConfigureType
  var storyboardName: String { get }
  func configure(injected: ConfigureType)
}

extension SomeName where Self: UIViewController {

  static func create(injected: ConfigureType) -> Self? {
    let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
    let controller = storyboard.instantiateInitialViewController() as? Self
    controller?.configure(injected: injected)
    return controller
  }

}

class SomeVC: UIViewController, SomeName {
  struct Params {
    let title: String
  }
  typealias ConfigureType = Params

  var storyboardName: String { return "Milestones" }

  func configure(injected: Params) {
    self.title = injected.title
  }
}

// Anywhere else
let params = SomeVC.Params(title: "My Title")
let controller = SomeVC.create(injected: params)

assosciatedtype? We'll make a Swift dev out of you yet 馃槀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jessesquires picture jessesquires  路  3Comments

viktorgardart picture viktorgardart  路  3Comments

rizwankce picture rizwankce  路  3Comments

weyert picture weyert  路  3Comments

BasThomas picture BasThomas  路  3Comments