Cheesebaron
Cheesebaron

Reputation: 24470

ViewModel navigation with TabBarPresenter

I have made a HomeViewModel which contains some other ViewModels to be presented in a TabParPresenter. This works fine and the ViewModels associated Views are presented correctly in their corresponding tabs. However on of the ViewModels have a check in the ctor that tells it in when some conditions apply it needs to navigate to SettingsViewModel , which is not a part of the ViewModels contained in HomeViewModel. The navigation event gets caught by the TabBarPresenterHost, which is simply the Presenter of the application and if a TabBarPresenter is present and not null it is passed to the TabBarPresenter.ShowView() method. All this happens before the TabBarPresenter is done loading and SelectedViewController is set in ViewDidLoad. Hence the TabBarPresenter relies on using the SelectedViewController to push new Views I obviously get a Null Reference Exception.

In short what I want is:

if (conditionForShowingHome == true)
    GoToHome();
else
    GoToSettings();

Then when inside SettingsViewModel I have set the stuff I need when going back you return to HomeViewModel.

What breaks this is that the ViewModels are loaded before the View is done loading and the navigation event is executed before the View is done loading. Any ideas how to go around this?

Upvotes: 1

Views: 125

Answers (1)

Stuart
Stuart

Reputation: 66882

I'm afraid that putting this code inside a ViewModel constructor is likely to lead to problems.

The ViewModel constructors are called during navigations - and not all platforms will support starting new navigations while existing ones are still in progress.

To workaround this, I personally opt to put this type of behaviour in the code before the navigation.

Upvotes: 1

Related Questions