Multiple child view controllers inside a uicontainerview (container view) - ios

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.

Related

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

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.

Embed segue - switching initial UIViewController and the contained UIViewController dynamically

What I need to do is basically build a container(view controller) that can change its child view controller dynamically and also set it's initial view controller dynamically.
I never used the Embed segue before so I thought I'll give it a shot.
However, using it seems to allow me to change the child view controller dynamically using a custom segue between the children view controllers but the initial view controllers seem to be fixed to the one I dragged the segue to in the StoryBoard(The custom segue here would be something alone these lines).
I know I can achieve what i'm looking for by creating x custom segue (where x is the number of children VCs I need) from the container view controller directly to the children and just calling these segues in code based on my needs.
But if that's the only way, what's the reason for using the "Embed" segue, is it only for really simple scenario's ?
An embed segue is not just for really simple scenarii. It can get pretty complicated. A major purpose is to cleanly separate code related to different concerns, that may still coexist on the same screen, into different view controllers. For instance, you could have an authentication controller and a preferences controller, both embedded into a single profile controller.

Building a slide out menu with container view controllers and the Storyboard. I'm very confused

I'm interested in building a slide out sidebar for a view controller, and I've done a lot of reading on how to implement it. This tutorial, as well as virtually all the others go over creating child view controllers, which seems like a great way to go about it. However, it's always done in code for examples.
This post goes over how to create container view controllers in the Storyboard, but it left me confused how exactly container views work in the Storyboard, and there seems to be little in the way of explanations online.
Finally, this post went over how to implement a slide out menu with container view controllers and a Storyboard. Sounds perfect right? Well, in reading it I was confused how the author seems to use multiple container views. I thought there was supposed to be one that encompassed all of the subviewcontrollers. Is it implemented correctly in that post?
Basically, I'd love an explanation on how to implement multiple view controllers using container view controllers via the Storyboard. It's my understanding from the first tutorial that you have the view controller that contains everything, and then add view controllers to that container and then they all work together via delegates. Achieving this in Storyboards is leaving me scratching my head.
Adding a container basically adds a child view controller and its view to the main controller. I think you are not clear on two things here. First, each container can be linked to only 1 controller/view pair. So, if you want to have more than one child controllers, you must have more than one containers. Secondly, communication between parent and child controllers is handled by 'embed segues', not by delegates.

Using multiple copies of the same view controller in a storyboard

I have the following setup in my app:
My initial view controller is a UITabBarController.
the tabs:
1)UINavigationController->PostListVC
2)UINavigationController->CategoriesListVC
3)UINavigationController->PostListVC
4)UINavigationController->PostListVC
5)UINavigationController->MoreViewController
As you can see, 3 tabs contain the same viewController class, but should not contain the same view controller object - the view will display different information based on information he gets form the AppDelegate.
What I did is I created 5 UINavigationControllers, connected them to the uitabbarcontroller, then created a rootViewController segue for 3 of them to the same PostListVC View - that way I don't need to maintain 3 designs of the same view.
The problem that I get is that only the first PostListVC gets created properly ( the leftmost in the tab bar ) - the other tabs that point to a PostListVC just show a black screen.
I've tried to illustrate the way I wire-up the storyboard using a 3-tab example:
As you can see, both the upper-most and lower-most views are connected to PostListVC.
I do not know what the issue is. I assume I'm using storyboards somewhat wrongly.
Does anybody know how I can fix this?
Thanks!
EDIT:
I have created a simple, example project (Xcode 5) that illustartes this issue:
http://www.speedyshare.com/Srwfg/TabBarProblem.zip
EDIT 2:
A modified version of the example, showing the problem with the offered solution:
http://speedy.sh/JkdGC/TabBarProblem-2.zip
There is no way to create different tabBarItems with this method, and there's no way to place the barItems so that they're not in a row - even if you try to chagne the order of segues.
As you said you need three different instances of PostListVC then you should create three different viewcontrollers of type PostListVC and connect each tab to its own. The class is the same but each tab gets its own instance.
I have got your example program to work BUT I don't know if the solution will work for your full project. Hopefully, it will put you on the correct track.
The solution is to have ONE (1) Navigation Controller / embedded root view but TWO (2) segues from the Tab Bar Controller. Here's the picture:
It looks like there's a problem with multiple UINavigationControllers linking to the same UIViewController. But no problem with the same UINavigationController linking to the same UIViewController provided they are instantiated separately through the UITabBarController.

UzysSlideMenu: how to use several view controllers?

I just came across a slide-in menu I really like: https://github.com/uzysjung/UzysSlideMenu
I would like to use this menu for an application that uses several view controllers (UIViewControllers and UINavigationControllers).
In Xcode, I created a single view application and made the view controller (MenuViewController) show the menu, like the creator did in his example project. I added more view controllers to the storyboard and connected them via segues to the MenuViewController. Upon selecting a menu item, these segues are triggered and the selected view is shown - so far so good.
But now, I run into the following problem:
All my view controllers are shown in full screen. That means that VCs that get segue'd in the viewport don't show the menu, because it's just not implemented there. I could put the menu in every VC, but that doesn't seem to be the right way to do it (even if I use some custom delegate method that every controller calls, like putMenuInViewController:(UIViewController *)target). I think I need something like a global singleton for the menu and make it appear in every view controller, but I have absolutely no idea on how to it or what to google for.
Any points into the right direction are greatly appreciated :)
I think you need to implement one root view controller with this menu as singleton, and add other view controller as child view controller to it.
I wrote a post about it, you can find it here:http://antrix1989.blogspot.com/2013/09/uiviewcontroller-as-singleton.html

Resources