MVVMCross support for Xamarin.iOS Storyboards

There is at least one sample published showing the use of Storyboards – the rather oddly named ehhttps://github.com/slodge/eh

This sample worked by:

Using approaches like this it’s pretty easy to add Mvx Data-Binding to an application that is primarily driven by the Storyboard.


Alternatively, if developers would prefer to let the Mvx ShowViewModel navigation system control the flow of screens – but would also prefer those screens to be designed within a storyboard, then this is possible by developing a normal MvvmCross application, but using a custom Presenter which loads ViewControllers from the storyboard.

In v3.1.1 of MvvmCross, you can do this at the ViewsContainer level:

  • override a class MyContainer from MvxTouchViewsContainer.cs
  • override the method protected virtual IMvxTouchView CreateViewOfType(Type viewType, MvxViewModelRequest request) – see https://github.com/MvvmCross/MvvmCross/blob/b8545752f28f4e569efeaa397c3085b0373e4d8b/Cirrious/Cirrious.MvvmCross.Touch/Views/MvxTouchViewsContainer.cs#L40
  • in this override, load your Storyboard-based ViewControllers:

     protected override IMvxTouchView CreateViewOfType(Type viewType, MvxViewModelRequest request)
     {
         return (IMvxTouchView)UIStoryboard.FromName("MyStoryBoard", null)
                                           .InstantiateViewController(viewType.Name);
     }
    
  • create your MyContainer during Setup

    protected override IMvxTouchViewsContainer CreateTouchViewsContainer()
    {
        return new MyContainer();
    } 
    
  • that should just then work…

Leave a Comment