What Is Container View in iOS 5 Sdk

What is Container View in iOS 5 SDK?

The container view is a view that you can drag into one of your view controllers that you already have in your storyboard (we'll call this viewControllerA). You automatically get a view controller connected to this view via an embed segue. This new view controller has its frame set, so that it's the same size as the container view -- if you resize the container view, the controller will automatically resize as well. So, if you want, you can drag in multiple container views into viewControllerA, and each will have its own view controller. In code, if you need to access these embedded view controllers, they can be accessed from viewControllerA.childViewControllers -- that will give you an array of any embedded view controllers that you have.

There is a discussion of these container views in the WWDC 2012 Session Videos video called "Adopting Storyboards in Your App".

what is the difference between view and container view in iOS design?

UIView object claims a rectangular region of its enclosing superview (its parent in the view hierarchy) and is responsible for all drawing in that region ...

Container View defines a region within a view controller's view subgraph that can include a child view controller.

Getting reference to view controller of the container view

You have your answer already. The container view is a view, not a view controller. It just defines the frame into which the embed segue will put the child view controller's content view.

When the system creates the child VC, it will call your prepareForSegue method, and at that point, segue.destinationViewController will contain the child view controller that is contained in the container view.

xcode storyboard Container View - How do I access the viewcontroller

There is another solution by specifying an identifier for the embed segue(s) and retrieve the corresponding view controllers in method prepareForSegue:

The advantage of this way is that you needn't rely on a specific order in which your child view controllers are added due to the fact that each child view controller is embedded via an unique segue identifier.

Update 2013-01-17 - Example

- (void) prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
// -- Master View Controller
if ([segue.identifier isEqualToString:c_SegueIdEmbedMasterVC])
{
self.masterViewController = segue.destinationViewController;
// ...
}
// -- Detail View Controller
else if ([segue.identifier isEqualToString:c_SegueIdEmbedDetailVC])
{
self.detailViewController = segue.destinationViewController;
// ...
}
}

c_SegueIdEmbedMasterVC & c_SegueIdEmbedDetailVC are constants with the corresponding ID of the segue IDs defined in the storyboard.

From within a view controller in a container view, how do you access the view controller containing the container?

You can use the prepareForSeguemethod in Vc1 as an embed segue occurs when the ContainerViewController is made a child. you can pass self as an obj or store a reference to the child for later use.

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString * segueName = segue.identifier;
if ([segueName isEqualToString: @"embedseg"]) {
UINavigationController * navViewController = (UINavigationController *) [segue destinationViewController];
Vc2 *detail=[navViewController viewControllers][0];
Vc2.parentController=self;
}
}

Edit: minor code fix

Same size of Container View and its child view

Thanks to your answers, I got pointed in the right direction - specifically, that this is not possible the way I thought it was.

There is no way to have constraints between the views of a parent view controller and a child view controller - neither in IB, nor in code

As far as I can see, there are two ways to solve this:

  1. Don't use a second UIViewController. Make the root view of your child view controller an actual subview of yourself, and just have a single UIViewController.

  2. Do not use auto layout. Manage the frame of your child view controller's view manually, and have delegate callbacks back to your parent view controller where necessary. In those delegate callbacks, your parent view controller can react to size changes of the child view controller's view.

IOS: View or container view

You should be more clear with your question. From my understanding ...

When people talk about container views, they usually mean just a plain old UIView that contains other views. Using a view in that way lets you move all the views it contains as a group, so that their positions relative to each other are maintained. It also makes it easy to hide all the contained views as a group.

Adding subviews for small applications, will not consume much memory. While if you are going for huge applications, you must maintain a state, so that which view will be added to container view as subview at each state of your application.

for example:


state 1 - login

state 2 - Dashboard

state 3 - People VC

Each state points to each viewController. So you have to maintain a state machine for states and viewController in your application.

Passing Data to view controllers that are embedded in container views

In your Storyboard, when you embed a VC in a ContainerView, you also see a "segue" connecter. When the root VC loads, you will get a call to prepare for segue for that.

Give each storyboard-created segue an Identifier - such as "infoViewEmbedSegue", "feedViewEmbedSegue", etc.

In your root VC, I'm guessing that

var infos: BusinessProfilesDetails!
var feeds: BusinessProfilePostsFeed!

are variables to reference the content of infoView? If so, in prepare() you want to:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

// get a reference to the embedded PageViewController on load

if let vc = segue.destination as? BusinessProfilesDetails,
segue.identifier == "infoViewEmbedSegue" {
self.infos = vc
// if you already have your data object
self.infos.otherUser = theDataDict
}

if let vc = segue.destination as? BusinessProfilePostsFeed,
segue.identifier == "feedViewEmbedSegue" {
self.feeds = vc
// if you already have your data object
self.feeds.otherUser = theDataDict
}

// etc

}

Now you'll have persistent references to the actual View Controllers embedded in your Container Views, in case you want access to them in other parts of your root VC, e.g.:

@IBAction func btnTapped(_ sender: Any) {
self.feeds.otherUser = theDataDict
}


Related Topics



Leave a reply



Submit