iOS/Swift: one Container View to hold 3 possible View Controllers - ios

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)
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?

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.

Related

What are the benefits of using a UINavigationController over adding a view with addSubView?

Let's say I have two pages with two UIViewControllers, UIViewController1 & UIViewController2.
If I want to show a UIViewController2 on top of UIViewController1 I have three options:
using UINavigationController pushViewController.
using presentViewController.
addSubView : UIViewController1.view.addSubView(UIViewController2.view)
If I need to a transition between my views, I prefer the third option because it gives me much more control over the views.
Is there any difference between these three options in terms of performance?
Before iOS 6 you were not supposed to do option 3. View controllers were meant to control the entire screen. In iOS 6 Apple added support for parent and child view controllers. You could make another view controller your child and add it's content views to yours.
If you are going to use option 3 then that's what you need to do. If you don't you will have a variety of problems.
There is even support for parent/child view controller built into storyboards. You can add a container view to a view controller, and then control drag from the container view onto another view controller's scene. When you do that the system creates an "embed segue" that sets up the child view controller inside the container view and wires up the parent/child relationship for you.
Your first 2 options are for when you want the new view controller to replace, or at least cover, the first view controller. Option 3 is for when you want a region of your screen to be managed by another view controller.
Option 3 (using a child view controller) does mean that both view controllers will be active and in memory at the same time, so you can't release the covered view controller's data storage while it's inactive like you can in a push or modal presentation. However unless your view controllers hold and present huge data structures this isn't much of a concern. In both a push and a modal presentation the covered view controller sticks around in memory anyway, waiting to be uncovered. You have to take special steps in order to free any memory while a view controller's view is covered and then reallocate it when it is displayed again - something that is unusual.
Just for two view controllers, it will not create major difference. UINavigationController is mainly used to maintain navigation stack.
But as you are having just two view controllers, you can use alternate way also.
If you are looking for transitions with NavigationController, you can use UIView Transitions for customisation of push pop animation.
Please refer following links for UIView Transitions
https://www.objc.io/issues/12-animations/custom-container-view-controller-transitions/
http://www.raywenderlich.com/86521/how-to-make-a-view-controller-transition-animation-like-in-the-ping-app
https://www.captechconsulting.com/blogs/ios-7-tutorial-series-custom-navigation-transitions--more
http://applidium.github.io/ADTransitionController/

Multiple child view controllers inside a uicontainerview (container view)

I would like to know how to embed multiple view controllers as child controllers of a container. I have tried to follow a few online tutorials but they all only use 2 controllers, not multiple. I have tried to convert their code to use more than 2 but have been unsuccessful. That is problem 1.
I found this great repo with very simple and easy to understand code for embedding 2 view controllers: https://github.com/mluton/EmbeddedSwapping
I tried to convert it to 3 but have not had any luck.
Problem #2 is that using this method I have not been able to navigate from one child view controller to another. I cloned the project and tried placing buttons on the child view controllers and then cntrl-drag to the next child to create a segue as one would do normally but no navigation happens when the button is tapped.
What I am trying to do in my project is display a view controller (the Start Screen) in a container view. I have a button on this view controller (Start Screen) which has a segue to Step 2 View Controller. I want Step 2 View Controller to display in the same container that Start Screen is. Then there is a button from Step 2 which goes to Step 3 and again I want it displayed in the same container view.
Are there any code samples online that do this which I can study? And/or do you happen to know how yourself and can share? I have been at this 3 days and no method I can think of has worked.
Although what you want to do is possible it's a pain to get working, ChildViewControllers are meant to be a one to one relation. If you want more you would have to add an intermediate view which connects to all the views. This intermediate view would be the one-one relation to your containerviewController.
However, based on your description I'm thinking you could fix this in a way more easy way. The flow you are describing is a typical navigation flow. Add a UINavigationController and connect that to your ContainerView, and just build your navigation stack like you would with a normal Navigation flow.

What is the difference between a container view and a container view controller?

I've just begun learning iOS programming and do not seem to understand the difference between these two terms.
I've been learning about container views and the apple documents constantly refer to a 'container view controller'. However, I see no class called UIContainerViewController. The storyboard lets me create a specific type of view known as a container view by the means of drag and drop and a standard view controller is created automatically with the creation of this container view and an embed segue is attached.
1 - Is this newly automatically created view controller the container view controller, since it looks like it is responsible for handling the content displayed in the container view that I just created?
2 - or is the original view controller which contains the container view called the container view controller?
3 - The apple document defines a container view controller with this statement - "A container view controller contains content owned by other view controllers." I don't quite understand what it means. Could someone explain it to me?
If there's any additional information required, please leave a comment and I will edit my question to include it.
To answer my own question a bit more systematically,
1 - The newly created view controller is not the container view controller. It is to be treated as an independent view controller. The embed segue specifies precisely that, a segue from the original view controller to the new one. However, the container view is still part of the old view controller and only indicates which other view controller's views will be displayed there.
2 - Yes. This is because the original view controller contains the container as a view. It is ultimately responsible as to choosing which other view controller's views is displayed in the container. Hence it performs the duties of a controller for that container.
3 - "A container view controller contains content owned by other view controllers" : This definition is now made clear. The original view controller displays views that are actually part of (owned by) an other view controller. Hence, as per the definition, the original view controller is the container view controller, having the newly created view controller as the child view.
In short, a container view controller lets you put view controllers inside other view controllers. the storyboard just provides you with a convenient tool that will cause a view controller to be placed in another view controller automatically. if you were to do it in code, you would need to create both view controllers and remove the view of the one vc and place it in the other, all the storyboard is doing is saving you that hassle.

Storyboard - how to make multiple containers embed to the same view controller?

Is there a way for multiple container view's on different scenes to point to one same scene or view controller ? The way I see it, every container has its own embedded scene. What would be the strategy here ? To use the same class on each embedded scene ?
You can hook up multiple container views to the "same" embedded view controller. Control-drag from each container view to the view controller and, when the popup menu appears, choose “embed”.
However, I put "same" in quotes because you need to understand what's going on. At runtime, each container view will create a new instance of the embedded view controller, created by deserializing that part of the storyboard once for each container view. They won't all share the same instance. If you want to keep one persistent view controller that you move around from container to container, you need to do that yourself in code.
Yes, just ctrl-drag from the container to the content view controller and select "embed" You'll also probably want to delete the unreachable view controllers from the storyboard.

accessing 2 viewControllers

I am creating an ipad application, I two view controllers the size screen of an iphone 5 and I would like to show both of them on the ipad screen, as two distinct UIViewControllers though. Is there a way to do it?
I have tried to alloc the second viewcnotrller in the viewdidload of the first, but what I notice is that it alloc the first and the second, but the first is not accessible any more (it looks just like a still image).
You can do it very easily in the storyboard. Just add 2 container views (next to the regular UIViews in the object list) to your controller's view, and size them how you want. You will automatically get 2 view controllers connected to the container views by an embed segue. Just change the class of these 2 controllers to your custom classes, and you should be good to go. If you need to get a reference to these controllers from the main controller, you can get it from the childViewControllers property. Your main controller (assuming it's the initial controller) and the 2 child controllers will all be instantiated at start up with no code necessary.
Check out view controller containment, in which you have a container view controller, which then can load one or more children view controllers. Also see the relevant section in the View Controller Programming Guide. Also see the WWDC 2011 session, Implementing UIViewController Containment.

Resources