Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How to dynamically change DetailView in SplitView template?

I've got a SplitView template project. I want to change DetailView on user selecting item in RootViewController.

In fact I can't just change what's inside that view (DetailView) like it's done in the template (when you check "use core data storage" on creating project). I want to switch between whole views.

I've tried that:

AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.detailViewController setView:curretnDetailViewController.view];
appDelegate.detailViewController = currentDetailViewController;

By the way I don't understand why do I need line#2 (..setView:..)? Why assigning detailViewController (line#3) is not enough? - Code looks awful=(

But anyway that doesn't work exactly as I want. Everything except detailView disappears. Like I've changed the main view, not just DetailViewController's

I guess I should change something in splitViewController but didn't find out what=(

Thanks for your attention.

like image 701
leshka Avatar asked Apr 12 '11 12:04

leshka


1 Answers

I too had your questions about a week ago and I too got little help in this forum. I hope I can change that for you.

First did you know apple has a Multi Detail View example? It is here Apple Examples

You have to understand what a splitviewcontroller is to get the idea of what Apple is doing. Think of the splitview controller as a container within your device window. This container has 2 compartments, a thin left hand side a bigger right hand side. Now to change over either side you have to replace this compartment with another compartment of similar size and nature. You cannot put in there a compartment that does not fit or that does something fancy cause it will simply not fit.

In technical talk find this below code in the example. The splitview controller has an array of 2 views and you can change the views in this array. Thus in this array is 2 views the left is called navigationcontroller and the right is detailviewcontroller. If you alloc and initiate a view and add it to either of these two then this view will replace the current view in the splitviewcontroller.

NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;

Now to your second part to your question - which I think is from the left hand side you want to be able to always be able to choose something and be able to change the right hand side....am I right?

I have read three different ways of doing this and not seen anyone who says one is better than the other.

  1. set up a notification - whereby when you click on something on the left a noticiation is sent to the right hand side.
  2. when you start with the bottom or root view of the left hand side you will have a reference to the right hand side, if you add subviews etc to the left hand side you always pass along a reference to the right hand side.
  3. The way I did it was to set up a protocol in the appdelegate or in the rootview delegate, then on a view that I want to talk to the right hand side it uses this protocol and my right hand side implements this protocol. This last one I learnt as it is a preferred method of getting modal views to talk to other views.

I am not an expert so I have not added code for each of the 3 choices above, what I suggest is that you do a little bit of googling for the topics and you will see examples. If you get really stuck on one let me know and I will try to find again with google the examples that I have found in the past. The least complicated I think is No.1.

I hope this get you going in the right direction.

like image 171
darbid Avatar answered Nov 04 '22 12:11

darbid