Container view action - ios

I'm using a container view (as suggested in this tutorial: http://spin.atomicobject.com/2015/07/21/ios-container-views/) to share a view between 2 view controllers in a storyboard.
In my container view I have a UIButton that I want to trigger a specific action in my first view controller and another action in my second view controller. How is this possible? Also can I wire up outlets in my view controller to my container view? I have not come up with a way to do that.

Your container view is "owned" by some ContainerViewController, I assume? Can't it tell to its child view controllers what they should do, when the button on container is pushed, since it is owned by the same VC?
When button is pushed it calls some method that tells "child" view controllers what to do... for example:
// On ContainerVC.m
- (void)buttonPushed:(id)sender {
[self.firstVC doYourThing];
[self.secondVC doYourStuff];
}
Hope I get the right idea of yours

Related

Have 2 container views. Need to change one to a different view controller

So I have a main view controller...
and two view containers. Vcon1 and Vcon2
I have them talking to each other via delegates however I'm not sure how to change the view controller displayed...so a tap on a button in Vcon1 will change the Vcon2 to have a new view controller...
I have a navagation controller in Vcon2 however I'm not sure I want a "stack" of view controllers in there...just want to swap out one for another.
Any suggestions appreciated..
If both of them have been added as subviews on the parentController's view, it's just a matter of calling bringSubviewToFront.
Else you can remove one of the view controllers, and proceed to add the other one. You can read more about view controller containment at objc.io

Pass data between View controllers without using prepareforsegue

I have a table view which is embedded in a container view on my main view controller. In my main VC I have a Navigation Bar which has a bar button item that goes to another View controller, which for demonstration purposes I'll call View controller 2. When I hit save on view controller 2 I have an unwind segue to my main vc (the one with the container view). My question is how do I then pass that data to my container view's child.
NOTE: I cannot use NSUserDefaults as I have a custom class which I want to transfer.
I think you should add a delegate to handle this.When 'unwind segue ' was called,just call the delegate for your child view controller. This is the best way.
If you need the data to be shared by all views, you could consider a singleton class - effectively global data.
Here's a tutorial which explains how to use it

Should Storyboard References work with Container Views?

I just tried it out and it doesn't seem to work, see picture. However, there is error or warning, and since a reference is a view controller - should it be possible?
Setting up a parent/child relationship with a container view is easy.Just create a container view inside the parent ViewController, create the child view controller scene, and then control-drag from the container view to the child view controller to create the embed segue.
If you want to swap the child view controllers depending on some condition, i.e. multiple child view controllers, just create a custom segue. This custom segue is named Empty and is a subclass of UIStoryboardSegue with empty perform method.

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.

In ios 6, how do you use pass data between View controller holding 'Container View' object and Table View controller that is embedded in it?

I have a view controller with a label and textfield. I also added a container view which points to another table view controller with one section and 3 rows, basically static table view. I am unable to find any documentation / example which tells you how you pass data between View controller holding the Container View and Table View container embedded in the container view. I want both sided communication ?
The controller embedded in the container view (in storyboard), is automatically added as a childViewController of the controller in which the container view is added. Just to make sense of what I mean, add this line, in your viewDidLoad method of the base controller :
NSLog(#"children : %#", self.childViewControllers);
So lets say in VC1, you add a container view with an embedded controller VC2 (your tableViewController), then the above statement will log VC2 as a child of VC1. To access VC2 from VC1, you just use [self.childViewControllers objectAtIndex:0], and to access VC1 from VC2, just use self.parentViewController.
Hope this helps
If you set things up in a storyboard, you use segues. Just like most everything else in storyboards.
See Access Container View Controller from Parent iOS
There are new properties on UIViewController -childViewControllers and -parentViewController. You could use those.
Alternatively you could set up a relationship yourself. Be weary of retain cycles. Perhaps the parent owns the child and the child has a weak reference back to the parent.
[self.childViewControllers lastOject] or [self.childViewControllers objectAtIndex:index]; , depending upon how many child VC's you have.

Resources