LoginSignup
4
2

More than 5 years have passed since last update.

ObjectをつかってViewControllerがインスタンス化された際に自動で依存性を解決する

Last updated at Posted at 2017-06-07

ObjectをつかってViewControllerがインスタンス化された際に自動で依存性を解決する

https://github.com/rambler-ios/Generamba
全然知らなかったんだけど、generambaを使っていてテンプレートに同じようなコードが含まれていたので気づいたw
めっちゃ便利だったので書いてみる

セットアップ

  • ObjectをドラッグアンドドロップでViewControllerに設置する

スクリーンショット 2017-06-07 16.40.14.png

Configurator(依存性を注入するクラス)を定義

  • NSObjectを継承したConfiguratorを定義する
import Foundation

class Configurator: NSObject {
}

ConfiguratorをObjectに紐付ける

  • custom classに先程定義したConfiguratorを紐付ける

スクリーンショット 2017-06-07 16.41.13.png

ViewControllerとConfigurator/ViewControllerを紐付け

スクリーンショット 2017-06-07 16.51.06.png

Configuratorで依存性を解決


import Foundation

class Configurator: NSObject {
    @IBOutlet weak var viewController: ViewController!

    override func awakeFromNib() {
        super.awakeFromNib()
        configure()
    }

    func configure() {
         // ここで依存性を注入する
             viewController.model = SmapleModel()
    }
}

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2