1

earlier i was initializing viewmodel object inside viewcontroller, but then when i read about SOLID principles (D = dependency invsersion) , we should not expose model view inside viewcontroller. so i thought of if i can inject viewmodel inside viewcontroller during initialization (viewcontroller is already designed in storyboard file) then that should work.

something like... ViewController having custom init like... func init(with viewModel : ViewModel)

but is it possible ??

mfaani
  • 33,269
  • 19
  • 164
  • 293
Matrix
  • 7,477
  • 14
  • 66
  • 97
  • create a viewModel inside the viewDidLoad method of ViewController. like this `viewModel = ViewModel(para : type)` and all models communicate with only viewModel and ViewController communicate with viewModel, viewModel is the interface between the viewController and model class – SGDev Nov 10 '19 at 09:12

1 Answers1

1

With storyboards you can't have a clean cut initializer!

When you call a viewController storyboard.instantiateViewController(withIdentifier: "viewController") the SYSTEM will do its stuff and call

required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
}

You can never redirect that call to another init method. Yet for programmatically created viewController or nib created viewControllers you can redirect that call as shown above.


You can either go with xibs and create your own init methods or take a full programmatic approach.

I've discussed this in full here

mfaani
  • 33,269
  • 19
  • 164
  • 293