MVVM Model and iOS Storyboard

Can someone explain to me how to implement the MVVM pattern when a project includes a Storyboard?

In many examples, I saw that I need to use .xib files. And init ViewControllers :

 -(instancetype)initWithModelView:(ViewModel *)viewModel{ self = [super init]; if(self){ _viewModel = viewModel; } return self; } 

But with the storyboard I can't init ViewControllers , the storyboard does this for me. Should I use properties instead?

i.e.

 UINavigationController *nav = (UINavigationController *)[self.viewControllers objectAtIndex:0]; HomeViewController *hvc = (HomeViewController *)[nav.viewControllers objectAtIndex:0]; hvc.viewModel = viewModel; 

self UITabBarController .

+6
source share
1 answer

You can initialize the viewModel property in prepareForSegue:sender: method of your UIViewController

Here's a link to the excellent MVVM app with Ash Furrow's C-41 sample.

An example viewModel initialization in this application:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"showDetail"]) { NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; ASHDetailViewController *viewController = segue.destinationViewController; viewController.viewModel = [self.viewModel detailViewModelForIndexPath:indexPath]; } else if ([[segue identifier] isEqualToString:@"editRecipe"]) { ASHEditRecipeViewController *viewController = (ASHEditRecipeViewController *)[segue.destinationViewController topViewController]; viewController.viewModel = [self.viewModel editViewModelForNewRecipe]; } } 
+8
source

Source: https://habr.com/ru/post/1200206/


All Articles