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.
Related
I have three view controllers each under the same custom view controller class, and a page view controller. I want to be able to reuse these three view controllers but with different content on their subviews. However, when I try to instantiate one of these view controllers from the page view controller using [self.storyboard instantiateViewControllerWithIdentifier:identifier], with a method to find the subview by tag right after, the subview returned is null. Is there some way I can get the subview by tag right after instantiating the view controller programmatically?
A view controller's views don't get created until it is about to be displayed. They won't be created after the call to instantiateViewControllerWithIdentifier:
You should put code that accesses the view controllers views in viewDidLoad, viewWillAppear, or viewDidAppear.
You should not try to manipulate a view controller's views from an outside object. That violates the principle of encapsulation, an important principle of object-oriented design. (It also often doesn't work, as you found out.)
If you need do things to the views programmatically you should add one or more public methods to the view controller and call those methods to ask the view controller to make adjustments to its views.
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
I've two different views which have two different controllers(firstVC and secondVC). Initially these two views appear in different viewcontrollers. Now, for some reason they have to appear in one viewcontroller and make some changes on one view when some action is done in second view, I need to create another viewcontroller(thirdVC).
Can I just add firstVC.view and secondVC.view on thirdVC's view and create some delegate methods of firstVC and secondVC to be implemented in thirdVC so that I do not need to make changes to existing firstVC and secondVC?
A view controller whose view contains subviews managed by other view controllers is called a "container view controller". This is a supported pattern but you need to see Implementing a Container View Controller and add the child view controllers to their parent container view controller in addition to adding their views as subviews.
This ensures that 1. the parent controller retains the child controllers so that their views do not send messages to deallocated controllers and 2. correctly sends framework messages like rotation events are sent to the child controllers. If you do not use these view controller containment methods your child controllers will eventually break in surprising ways.
I have created and drawn out a custom UIViewController called AutocompleteVC in my Main storyboard. AutocompleteVC will be used in several different places/storyboards and with different dimensions.
For example, in my Transit storyboard, shown below,, there is a custom UIViewController called TransitVC, shown on the left. In TransitVC, I have a container view with an IBOutlet called autocompleteContainerView. autocompleteContainerView has a segue called autocompleteEmbedSegue to a generic UIViewController, shown on the right in red.
The goal is to have TransitVC hold AutocompleteVC inside autocompleteContainerView. But I'm having trouble getting this to work. The reason I want to do this inside a container view is so I can use autolayout to set constraints on it. Otherwise, I know how to do this purely in code.
I believe my approach might be flawed. What is the correct approach to do this if I want to maximize storyboard usage.
I'm not sure what you are asking. Setting up a parent/child relationship with a container view is very easy, exactly as you have outlined. Just create a container view inside the parent view controller, create the child view controller scene, and then control-drag from the container view to the child view controller to create the embed segue.
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.