Accessing An Instance Off A Class From Another

JSpringJSpring GBUniversity ✭✭

Hi,

I was wondering if someone would be able to provide me some advice as to how to achieve what I am about to explain.

Firstly, a screenshot to describe my structure at the moment:

View Controllers:
- DashboardViewController (Dashboard)
- TableSourceController
- MapViewController (MapView)
- MapViewDelegate

What I am trying to do is when a user selects a row in the table view take the row number and retrieve the data for that index, then based off that data call either a method in the MapViewController which will modify the map or modify the map directly. However since I am not manually constructing the MapViewController as it is all instantiated from the Storyboard I am not sure how to access the MapView or MapViewController methods.

Any help would be very greatly appreciated!

Thanks.

Tagged:

Posts

  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai
    edited December 2015

    If you are using Storyboards to wire up your navigation, you can intercept the transition from one controller to another by overriding the PrepareForSegue() method on the source controller like this:

        public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender)
        {
            base.PrepareForSegue (segue, sender);
    
            var myCustomTargetViewController = segue.DestinationViewController as MyController;
    
            if (myCustomTargetViewController != null) 
            {
                myCustomTargetViewController.SomeProperty = "Some Value";
            }
        }
    

    You can see a full example in our docs.

  • JSpringJSpring GBUniversity ✭✭

    Hi @rene_ruppert - the MapView is a container view within the DashboardView so there is no Segue otherwise I would have done this.

    To give you a usage example, when a user taps on a drive they recorded I want to place some markers on the Map. I already have the method in MapViewController but need a way to access it.

  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai

    @JSpring: I cannot see your screenshots. Also some code example would be helpful.

    If you want to get access to the view controller, you could also add a static member to it:

    void MyController : UIViewController
    {
      // Constructor used when created via storyboard.
      MyController(IntPtr ptr) : base(ptr)
      {
        Instance = this;
      }
    
      // This is your access to the controller.
      public static MyController Instance { get; private set; }
    }
    
  • JSpringJSpring GBUniversity ✭✭

    @rene_ruppert thanks - I went down the static route in a slightly different way by creating a 'Globals' class and upon the VDL being called assigned the instance of it to there.

    Thanks!

Sign In or Register to comment.