user3034944
user3034944

Reputation: 1741

Change Detail View controller on Master View controller item click UISplitViewController

I have a UISplitViewController, with a master viewcontroller which is a table and a detail view controller.

I have implemented it as so:-

MainViewController.cs

    public override void ViewDidLoad()
    {
       UpdateView(masterVC,detailVC);
    }
    public void UpdateView(UIViewController master, UIViewController detail)
   {
       this.ViewControllers = new ViewControllers[]{master, detail};
   }

This works fine. Now I want to change the detail view controller to another one when the user clicks an row in the master viewcontroller.

What I do is:-

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
  {
             var mainVC = StoryBoard.InstantiateViewController("Main_VC") as MainViewController;
           mainVC.UpdateView(mainVC.ViewControllers[0], newDetailVc);
    }

This does not do anything and does not change the detail view to the new one. How can I implement this?

Upvotes: 0

Views: 398

Answers (1)

JoeTomks
JoeTomks

Reputation: 3276

Your re-instantiating your master view controller rather than providing a pointer to the original that you loaded when the app launches, as such as far as the application is concerned your simply updating the detail view of a 'master' view controller that's only sat in memory.

When you launch your application in the MainViewController ViewDidAppear method create a static reference to itself like so:

public static MainViewController MVCPointer {get; private set;}

public override void ViewDidAppear(bool animated)
{
    base.ViewDidAppear(animated);

    MVCPointer = (self as MainViewController).
}

Then in rowselected you would do the following:

public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
       var mainVC = StoryBoard.InstantiateViewController("Main_VC") as MainViewController;
       mainVC.MVCPointer.UpdateView(mainVC.MVCPointer, newDetailVc);
}

There are much tidier ways of doing this, the above is just a quick way of adapting your current code, personally I store a reference to any navigation controllers inside a static variable inside of AppDelegate so I can access it anywhere, but that's primarily useful if you have an application with a lot of different viewcontrollers and navigation controllers.

The other way of doing what you want is to pass a reference of your mainviewcontroller to your tableviewsource class (if your using one)

that way you can simple do:

private static MainViewController MVCMain {get ; private set}

public void nameofyourtableviewsource (object tableviewdata, MainViewController cont)
{
      MVCMain = cont;
}

public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
      (mainVC.ViewControllers[0] as MainViewController).UpdateView(mainVC, newDetailVc);
}

Upvotes: 0

Related Questions