0

In an area of the app, I would like to show one of 3 possible View Controllers.

I am assuming I can do that by adding a Container View is such area (as shown below)

enter image description here

The questions are:

1) How do I wire the 3 children View Controllers to this Container View in the storyboard? Do I need to connect the Container View to a parent View Controller and then connect this to the 3 children?

2) Which is the Swift code to show one of the 3 View Controllers and hide the other 2?

Daniele B
  • 19,801
  • 29
  • 115
  • 173
  • 1
    https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html – Zell B. Aug 21 '15 at 20:37
  • Thanks for the link. However how do I wire the Container View to the View Controllers in the storyboard? Do I need to use this approach http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controllers with a ContainerViewController linking the ContainerView to the child View Controllers? – Daniele B Aug 21 '15 at 22:35

1 Answers1

2

I assume that when the user first enters the screen, one of the three view controllers will be there and the user has buttons to switch to a different view controller.

Attach that first VC directly to the container in the storyboard. The other two can be attached to the buttons that cause them to come to the foreground, or attached to the primary VC directly.

You will also need to make a custom segue class (or two?) that knows how to handle the transition from one VC to another.

--- Edit ---

Here is one option as a github repo. Note that if you want to transition between view controllers, you have a bit more work to do, but this should get you started.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72
  • In my case, there is actually no default VC. Which of the 3 VCs has to be attached to the container depends on a set of rules. I would prefer to include that logic outside the 3 VCs. Which architecture do you suggest in this case? Should I create a parent VC? And how do I connect the parent to the children? – Daniele B Aug 22 '15 at 00:07
  • In that case you can connect the three VCs directly to the parent VC (the one with the container view,) and give them each a named segue. You can programmatically perform the appropriate segue. OR, you can not bother with segues at all. Give each of the three VCs an identifier and programmatically instantiate them from the storyboard object. – Daniel T. Aug 22 '15 at 00:46
  • How do I wire the Container View to the 3 VCs in this case? – Daniele B Aug 22 '15 at 00:48
  • I put a sample project in the answer to help you out. – Daniel T. Aug 22 '15 at 01:16
  • Hi Daniel T., it looks like a new instance of the viewController child class is created for each segue. Any suggestion on how to reuse the same instances? – Daniele B Aug 25 '15 at 00:57
  • Store the data you need to reconstitute the view controller and pass it to the new instance. It's not a good idea to try to reuse VCs. – Daniel T. Aug 25 '15 at 01:28
  • Yes, true. But isn't it possible to remove the already created viewcontrollers, so that the don't pile up? – Daniele B Aug 25 '15 at 01:30
  • or maybe some way of clearing the parent view controller before adding a new child? – Daniele B Aug 25 '15 at 01:33
  • This isn't something you can just throw code at. You have to understand what is going on and why. You really need to read https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html – Daniel T. Aug 25 '15 at 01:55
  • I read the article, but I still don't have it clear. In our example, you add the child viewController to both the parent viewController and the containerView. Is there a particular reason to do so? in the article it just adds the child viewController to the containerView. – Daniele B Aug 25 '15 at 02:03
  • ah sorry, I am wrong, the child is also added to the parent viewController – Daniele B Aug 25 '15 at 02:05
  • Instead of making your own container, you should try using a tab view controller and just hide the tab bar. That might work better for you. – Daniel T. Aug 25 '15 at 02:09
  • Daniel T., I have just implemented it with the UITabBarController (by hiding the tabs with `self.tabBar.hidden = true`) and that way is very smooth and neat! thanks so much! – Daniele B Aug 26 '15 at 12:33