Andy Attebery
Andy Attebery

Reputation: 55

MvxViewModelRequest is null in ViewDidLoad

I'm using MvvmCross 3.0.12 in an iPad project. Currently, I'm getting a NPE in MvxViewControllerExtensionMethods.LoadViewModel, because touchView.Request is null. This is only happening in a view controller that is inheriting from MvxTableViewController; view controllers inheriting from MvxViewController load and display just fine.

I've set breakpoint in MvxTouchViewsContainer.CreateView -> MyTableViewController.Ctor -> MvxBaseTouchViewPresenter.Show -> MyTableViewController.LoadView; which have all referenced the same instance of the class. Then when I hit a breakpoint in ViewDidLoad, its a new instance and all of the properties including Request are null.

Is this a bug with Xamarin.iOS or am I doing something wrong?

Upvotes: 2

Views: 641

Answers (1)

Stuart
Stuart

Reputation: 66882

This sometimes happens for view controllers like table, tab bar and collection.

It's caused, I think, by something in the Objective C base class init referencing the View - and thus triggering ViewDidLoad before the C# construction is fully complete. This is a bit like what can happen in 'pure' c# if a base class constructor references a virtual method.

To check this is occurring for your app, put 2 breakpoints in your app - one in the ViewController constructor and one in ViewDidLoad - if ViewDidLoad is fired first, then you know this is the case.

The only way around this I've found is to code around this by triggering a second ViewDidLoad call in the constructor.

    public FirstView()
    {
        // need this additional call to ViewDidLoad because UIkit creates the view before the C# hierarchy has been constructed
        ViewDidLoad();
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        if (ViewModel == null)
            return;

        // ...

If it helps:

Upvotes: 1

Related Questions