Thursday, May 10, 2012

Presenting modal UIViewControllers in MonoTouch using MvvmCross

MvvmCross starts from the premise that all ViewModels are just "normal pages" - so in iOS/MonoTouch that means UIViewControllers presented using a UINavigationController.
To move away from this premise - towards tabbed displays, modal displays, split controllers, popups, etc - then you can adjust the Presenter logic within your MonoTouch app.
The presenter's job is to implement:
public interface IMvxTouchViewPresenter
    void Show(MvxShowViewModelRequest view);
    void Close(IMvxViewModel viewModel);
    void CloseModalViewController();
    void ClearBackStack();
    bool PresentModalViewController(UIViewController controller, bool animated);
    void NativeModalViewControllerDisappearedOnItsOwn();
The presenter used for your app is selected in AppDelegate construction - e.g. see how theTwitterSearch builds different presenters for iPhone and iPad.

Fortunately, for simple Modal support, one of the standard presenters available isMvxModalSupportTouchViewPresenter.cs
This presenter looks to see if the view being presented has the IMvxModalTouchView marker interface - it tests view is IMvxModalTouchView. If this interface is present, then it uses modal presentation for the view instead of "normal navigation".
To use this, change your AppDelegate code to something:
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        window = new UIWindow(UIScreen.MainScreen.Bounds);

        // initialize app for single screen iPhone display
        var presenter = new MvxModalSupportTouchViewPresenter(this, window);
        var setup = new Setup(this, presenter);

        // start the app
        var start = this.GetService();


        return true;
Then add the marker interface to your modal View(s):
 public class MyView : MvxBindingTouchViewController, IMvxModalTouchView
      // ....

No comments:

Post a Comment