Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM and pushing a ViewController: where to initialize the next ViewController and ViewModel, and push on the new view?

I'm thinking through the structure of a very simple ViewModel and ViewController for a test application. I have something akin to:

FirstViewController.m:

- (IBAction)launchButtonSelected:(id)sender
{
    [self.viewModel launchActionSelected];
}

FirstViewModel.m:

- (void)launchActionSelected
{
    // [todo] - Figure this out.
}

When the launchButton is selected in the FirstViewController, I want to make and present a SecondViewController.

My questions:

  1. Is there a solid rule of thumb for where should I create SecondViewController's ViewModel?
  2. Who should initialize SecondViewController?
  3. Where should I push SecondViewController onto the view hierarchy? (i.e. a navigation push or a modal presentation).

I was personally thinking:

  1. The ViewModel for SecondViewController will probably be created in its initializer. This always leads me down a confusing path: what if I want to pass information from FirstViewModel to SecondViewModel? Should I expose SecondViewModel as a public property on SecondViewController so I can get/set values on it?
  2. FirstViewController should create SecondViewController, and
  3. FirstViewController should push SecondViewController onto the screen.

My intuition considers this to be subpar: I'd like to isolate the presentation of ViewControllers a bit more, and have the app be more ViewModel-focused, but this seems difficult to do. (i.e. "push" ViewModels, not ViewControllers… but "push" is intrinsically related to the app's visual presentation, so perhaps that's the wrong way to think about it.)

like image 336
cbowns Avatar asked Feb 20 '14 23:02

cbowns


People also ask

Can you explain MVVM and how it might be used on Apple's platforms?

Understanding MVVMIt includes updating the table, the labels and animating the screen components. Business logic is the underlying process that handles communication between databases and the end-user interface. It guarantees the model is updated, the data is handled in a way designed and so on.

What is ViewModel in MVVM Swift?

The MVVM pattern introduces a fourth component, the view model. The view model is responsible for managing the model and funneling the model's data to the view via the controller. This is what that looks like. Despite its name, the MVVM pattern includes four major components, model, view, view model, and controller.

How do I create a ViewModel in Swift?

Go to the ViewModel Xcode project group, which will be empty, create a GameScoreboardEditorViewModel. swift file, and make it a protocol. Using protocols like this keeps thing nice and clean; you only must define data you will be using. Next, create an implementation for this protocol.


1 Answers

Great questions. It's important to remember that, on iOS anyway, MVVM is an emerging paradigm with emerging best practices. So the answer to your first question about hard-and-fast rules is that there aren't really any. I personally would create the SecondViewController's view model in the FirstViewController's view model so it can be configured in the context that it will be used (i.e.: if the new view controller is being pushed in response to a table view selection, the index path). Your answers to the other two questions are correct, at least in my interpretation of MVVM on iOS.

View models shouldn't have access to your views, and since MVVM formalizes the view and view controller as one unit, they shouldn't being creating or pushing view controllers, either. Hope that makes sense.

I've got an app I wrote on GitHub that uses MVVM. You can check it out here.

like image 186
Ash Furrow Avatar answered Oct 24 '22 19:10

Ash Furrow