Search code examples
iosxamarin.iosxamarinmvvmcross

How to access most top view controller in Xamarin iOS with Mvvmcross?


we want to find the most top ViewController in the viewstack to present a little popup. To do this we find out the NavigationController of the key window rootviewcontroller:

var root = UIApplication.SharedApplication.KeyWindow.RootViewController.NavigationController;
var ctrl = rootNav.TopViewController as ViewControllerBase; // our own VC
return ctrl != null ? ctrl.Popup : null;

This works in debug mode - in release mode the 'RootViewController' is null? Is MVX providing a different solution for this? How do we access to the UINavigationController?


Solution

  • The difference debug-to-release suggests to me that this is a Xamarin.iOS/MonoTouch effect - possibly to do with linker settings.

    It's probably worth chasing down what this issue is and trying to fix it.

    However, if you do want a quick fix - if you want to instead use MvvmCross's stored reference to the RootViewController, then you can do this in a Custom Presenter which inherits from MvxTouchViewPresenter - see MvxTouchViewPresenter.cs#L158