UIStoryboardSegue.DestinationViewController is null on first call but not second(in PrepareForSegue)

BillDwyerBillDwyer USMember

I am trying to pass data to a ViewController whose Segue is defined in my Storyboard. I am using the following code:

    public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
    {
        base.PrepareForSegue(segue, sender);

        if (segue.Identifier == "JobDetailSegue" )
        {
            int jobId = ((JobListTableViewSource)TableView.Source).Items[TableView.IndexPathForSelectedRow.Row].JobId;

            JobDetailViewController screen = (JobDetailViewController)segue.DestinationViewController;
            screen.SetJobId(jobId);
        }
    }

I was experiencing seemingly "random" errors while debugging, and I eventually noticed that when I inspected my segue object's DestinationViewController property, an exception was being thrown, but subsequent inspections would bring up the expected ViewController.

This is the exception that was being raised:

  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00016] in /Users/builder/data/lanes/1926/6916d0e3/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:541 
  at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00095] in /Users/builder/data/lanes/1926/6916d0e3/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:526 
  at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in /Users/builder/data/lanes/1926/6916d0e3/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:554 
  at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in /Users/builder/data/lanes/1926/6916d0e3/source/mono/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:62 
  at ObjCRuntime.Runtime.ConstructNSObject[UIViewController] (IntPtr ptr, System.Type type, MissingCtorResolution missingCtorResolution) [0x00000] in <filename unknown>:0 
  at ObjCRuntime.Runtime.GetNSObject[UIViewController] (IntPtr ptr) [0x00000] in <filename unknown>:0 
  at UIKit.UIStoryboardSegue.get_DestinationViewController () [0x00010] in /Users/builder/data/lanes/1926/6916d0e3/source/maccore/src/build/ios/native/UIKit/UIStoryboardSegue.g.cs:149 
  at ***************App.ViewControllers.JobList.JobListTableViewController.PrepareForSegue (UIKit.UIStoryboardSegue segue, Foundation.NSObject sender) [0x0000a] in c:\dev\Research and Development\Apps\Trunk\**************.App\ViewControllers\JobList\JobListTableViewController.cs:96 
  at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/1926/6916d0e3/source/maccore/src/UIKit/UIApplication.cs:63 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0001c] in /Users/builder/data/lanes/1926/6916d0e3/source/maccore/src/UIKit/UIApplication.cs:46 
  at ****************.App.Application.Main (System.String[] args) [0x00135] in c:\dev\Research and Development\Apps\Trunk\*******************\App.cs:66 }

I changed my code to the following, and I can process the Segue without any issues:

    public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
    {
        base.PrepareForSegue(segue, sender);

        if (segue.Identifier == "JobDetailSegue" )
        {
            try
            {
                JobDetailViewController thisIsCrayCray = (JobDetailViewController)segue.DestinationViewController;
            }
            catch (Exception)
            {
                // The first time it triest to access the DestinationViewController, 
                // it will throw and exception, but the second time is fine
            }

            int jobId = ((JobListTableViewSource)TableView.Source).Items[TableView.IndexPathForSelectedRow.Row].JobId;

            JobDetailViewController screen = (JobDetailViewController)segue.DestinationViewController;
            screen.SetJobId(jobId);
        }
    }

What the what? Any idea what might be going on here? It seems like the UIKit.UIStoryboardSegue.get_DestinationViewController () might be at fault here, but I have no idea how to diagnose this.

Thanks in advance.

Bill

Answers

  • BillDwyerBillDwyer USMember

    bump

    Any thoughts from the Xamarin folks? I hope I described the problem well enough.

  • BillDwyerBillDwyer USMember

    I'm leaving this question here, in case it adds some context for others who might experience the same. I ended up programatically loading my ViewController so I didn't have to rely on this Segue that involved weird behavior.

Sign In or Register to comment.