0

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?

user3034944
  • 1,541
  • 2
  • 17
  • 35

1 Answers1

0

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);
}
JoeTomks
  • 3,243
  • 1
  • 18
  • 42
  • Thanks for the reply. In your first solution, what is MVCPointer? – user3034944 May 15 '17 at 10:52
  • Sorry completley glossed over assigning it a class, I've just fixed the code snippet above, should make sense now. – JoeTomks May 15 '17 at 10:53
  • I tried the first approach, but it still works the same way as my code did. The detail controller does not change – user3034944 May 15 '17 at 11:18
  • The debugger does show the detail view controller as the new one but does not display in the detail part. This was a similar behavior with my approach as well – user3034944 May 15 '17 at 11:27
  • can you give me an example/sample that I could refer which implements the same functionality ? – user3034944 May 16 '17 at 03:20
  • Something like xamarins guide on multiple detail splitviews might help. https://developer.xamarin.com/recipes/ios/content_controls/split_view/use_multiple_detail_controllers/ – JoeTomks May 16 '17 at 08:08