How to handle navigation in iOS when using MVVM

I introduced ViewModels in my last iOS app, but I am stuck in several places. I wonder if anyone here can send me in the right direction.

Basically, How / Where should handle navigation and error dialogs.

For example, I currently have a registration process consisting of RegistrationViewController and RegistrationViewModel. When the registration button is used in the user interface, the register method is called in the RegistrationViewModel module. i.e. viewModel.register () from the controller

Question 1: How should navigation be handled? After successfully completing the registration of the web service, the application should go to one of several screens depending on some business logic. I am currently processing this in the ViewModel, invoking the navigation manager class. Navigator.goToSuccessScreen (). It is acceptable? I get the feeling that this should be handled inside the controller rather than through the ViewModel, but then all the business logic to determine where to look will be executed in the view controller.

Question 2: How to display a dialog box with an error? Assume that the registration call above failed. And the user interface should show the user the UIAlertView. Would it be ok to call Navigator.displayError ("this or that error message")? Or, again, should there be a way to redirect this back to the controller to show the message itself?

+4
source share

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


All Articles