IOS: How to dynamically change DetailView in a SplitView template?

I have a SplitView template project. I want to change the DetailView on the user by selecting an item in the RootViewController.

In fact, I canโ€™t just change what is inside this view (DetailView), as it was done in the template (when you check "use the master data store" when creating the project). I want to switch between whole views.

I tried:

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

By the way, I donโ€™t understand why I need line # 2 (..setView: ..)? Why is assigning a detailViewController (line # 3) not enough? - The code looks awful = (

But in any case, this does not work exactly the way I want. Everything except the detailView disappears. How I changed the main idea, not just

DetailViewController

I think I should change something in splitViewController, but did not understand that = (

Thanks for attention.

+6
source share
1 answer

I also had questions about a week ago, and I also helped a little in this forum. Hope I can change this for you.

First you know that an apple has an example with a few details? Here are Apple Examples

You need to understand what a splitview controller needs to do in order to understand what Apple is doing. Think of the splitview controller as a container in your deviceโ€™s window. This container has 2 compartments, the thin left side is the large right side. Now, in order to change the other side, you must replace this compartment with another compartment of the same size and nature. You cannot put a compartment there that does not fit or comes up with something, because it simply does not fit.

In a technical conversation, find this code below in an example. The splitview controller has an array of 2 views, and you can change the views in this array. Thus, in this array there are 2 types, the left one is called the navigationcontroller, and the right one is the viewviewcontroller. If you have assigned and initiated a view and added it to either of these two, then that view will replace the current view in the splitview controller.

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

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

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

  • configure the notification - when you click on something on the left, the message is sent to the right side.
  • when you start from the bottom or root view of the left side, you will have a link to the right side if you add subviews, etc. to the left side, you always follow the link to the right side.
  • The way I did this was to configure the protocol in appdelegate or in rootview, and then in the view I want to talk to on the right side, it uses this protocol, and my right side implements this protocol. This is the last thing I learned, as it is the preferred method of obtaining modal representations for communicating with other representations.

I am not an expert, so I did not add code for each of the 3 options above, I suggest that you work a little on the topics and you will see examples. If you are really stuck on one, let me know and I will try again to find examples from Google that I found in the past. It seems to me that the least complicated is No.1.

Hope this helps you move in the right direction.

+18
source

Source: https://habr.com/ru/post/885728/


All Articles