Custom init for UIViewController in Swift with interface setup in storyboard

后端 未结 12 2134
长情又很酷
长情又很酷 2020-11-27 05:08

I\'m having issue for writing custom init for subclass of UIViewController, basically I want to pass the dependency through the init method for viewController rather than se

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 05:17

    There were originally a couple of answers, which were cow voted and deleted even though they were basically correct. The answer is, you can't.

    When working from a storyboard definition your view controller instances are all archived. So, to init them it's required that init?(coder... be used. The coder is where all the settings / view information comes from.

    So, in this case, it's not possible to also call some other init function with a custom parameter. It should either be set as a property when preparing the segue, or you could ditch segues and load the instances directly from the storyboard and configure them (basically a factory pattern using a storyboard).

    In all cases you use the SDK required init function and pass additional parameters afterwards.

提交回复
热议问题