Reputation: 185
In my app I have a complicated View which contains:
A corePlot graph
A Collection View (controlled by Core Data)
and some other Views, (controlled by Core Data different request)
The items in the collection view, can be copy-dragged out to the Core Plot.
I think it would be nicer and easier to have several view Controllers:
one controller for the Core Plot, one for The collection view and so on. Because if I have only one view controller to control all of these items, it will be a huge file containing a lot of code and not well structured.
Could I use Container for this problem? or is it better to stick with the huge ViewController file? Or is there another solution? If yes can this solution be done in the storyboard?
Upvotes: 2
Views: 1756
Reputation: 9433
The Container View Controller seems perfect for what you want:
All the steps are listed in the link above, but to be short:
- you add the child view controller to the container with [self addChildViewController:childVC]
- then you set the view of the container [self.view addSubview:childVC.view]
- and then tell the child view controller that it is set in the container with [childVC didMoveToParentViewController:self]
Then if you want to switch to a new child view controller:
- Call [self addChildViewController:newVC]
and [source willMoveToParentViewController:nil]
, so the container know that you are going to switch the child view controller.
- then do the transition with:
[self transitionFromViewController:oldVC toViewController:newVC
duration:/*you duration*/
options:/*your options*/
animations:/*your animations*/
completion:/*your completion*/]
[oldVC removeFromParentViewController]
and [newVC didMoveToParentViewController:self]
to let the container know that the switch is done.Hope that helps.
Upvotes: 3
Reputation: 893
You can have different view controllers for your views. But, you have to try this hack since you want to present the content of all view controllers at the same time on a single scene.
Container view controllers are the view controllers which present the content of their children view controller. But, a container view presents only one child at a time. So, you have to create a view controller and add as many container views as many you want view controllers. I have attached a snapshot .
Hope this helps!
Steps to achieve this in a story board:
1) In the IB choose a view controller.
2) Add Container Views from IB to view hierarchy of view controller.
3) You can change the child of container view by simply deleting it and connecting your desired view controller.
4) Both the children of both container views are presented without any code in a storyboard.
For initialization of your view controllers and delegation b/w the I highly recommend you to review the sample codes in View Controller Programming guide
Upvotes: 1