ViewModelExtensions 1.2.1

ViewModelExtensions 1.2.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2015
SPMSupports SPM

Maintained by József Vesza.



A convenience library to inject view models into view controllers defined in Interface Builder.

Usage

First, mark your classes as view models by conforming to the ViewModelType protocol.

struct ExampleViewModel: ViewModelType {
    // Your implementation
}

To support view model injection, your view controller class must conform to ViewControllerInitializable:

extension ExampleViewController: ViewControllerInitializable {

    static func instanceWithViewModel(viewModel: ExampleViewModel) -> MainViewController? {
        if let instance = self.instance() as? ExampleViewController {
            instance.viewModel = viewModel
            return instance
        }

        return nil
    }
}

Important:

  1. Currently nibs are not supported. Your view controllers must be defined in a storyboard file.
  2. The view controller's storyboard ID must match the class name.

After these steps, you can initialize a view controller:

let exampleViewController = ExampleViewController.instanceWithViewModel(ExampleViewModel())

Installation