2

On the start of my iOS application (that I am building with Xamarin and MvvmCross), I want to immediately change UIViewController to a UITabBarViewController. My code:

public class MainViewModel : BaseViewModel
{
    public void Initialization()
    {
        ShowViewModel<TabLayoutViewModel>(); // Breaks here
    }
}

public class MainViewController : BaseViewController<MainViewModel>
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        this.ViewModel.Initialization();
    }
}

public class TabLayoutViewController : MvxTabBarViewController<TabLayoutViewModel>
{
}

On the line ShowViewModel<TabLayoutViewModel>() it throws an exception:

A TabBarViewController cannot be presented as a child. Consider using Root instead

I just want to push this controller on top of the stack. I know this is legal in plain iOS so there should be a way to do it with MvvmCross?

Drake
  • 2,679
  • 4
  • 45
  • 88
  • If it is not possible it is a bug. As you say yourself, it should be possible to push a TabBarViewController on top of the stack. If you can't find a way to do it. Please report a bug in the MvvmCross GitHub issues. – Cheesebaron Jun 08 '17 at 05:55

2 Answers2

3

Update: Since MvvmCross 5.0.4 it is now possible to show a TabBarController as a child. Just mark your TabBarController with [MvxChildPresentation].

See this PR to the source code..


Original answer:

A TabBarController is not meant to be presented inside a UINavigationController. What you can do is to change the root ViewController of your Window. To do so, you can add the MvxRootPresentation attribute above the TabLayoutViewController class.

If you do need to show tabs inside a UINavigationController, you might find this question relevant.

nmilcoff
  • 1,084
  • 5
  • 20
2

I had to do just that last week.

What I do to quickly resolve this is simple:

1) Create a custom presenter that inherits from MvxIosViewPresenter (https://github.com/MvvmCross/MvvmCross/blob/develop/MvvmCross/iOS/iOS/Views/Presenters/MvxIosViewPresenter.cs).

2) Override the ShowChildViewController method, using the original as model and comment these two lines:

if (viewController is IMvxTabBarViewController)
    throw new MvxException("A TabBarViewController cannot be presented as a child. Consider using Root instead");

3) Override the CreatePresenter method in Setup.cs:

protected override IMvxIosViewPresenter CreatePresenter()
{
    return new CustomTabChildMvxIosViewPresenter(ApplicationDelegate, Window);
}
Rafael de Andrade
  • 853
  • 1
  • 6
  • 11
  • This is the correct direction, actually in latest MvvmCross version, the limitation is already removed. – Felix Aug 17 '17 at 00:59