UIViewController in a UIView

16,455

Solution 1

Starting with iOS 5

"Container view controllers" have been added in iOS 5. You can add a view controller as a child of another one with addChildViewController:.
You also are responsible for adding its view to its parent's view.

Everything is well documented in the iOS SDK documentation: Implementing a Custom Container View Controller.

To add a child view controller:

childViewController.frame = ...
[self.view addSubview:childViewController.view];
[self addChildViewController:childViewController];
[childViewController didMoveToParentViewController:self];

and to remove it:

[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];

Prior to iOS 5

It's possible to load another view controller and add its view as a subview of another controller's view.

UIViewController *subController = ...
[self.view addSubview:subController.view];

Although it's not recommended by Apple's guidelines:

Each custom view controller object you create is responsible for managing all of the views in a single view hierarchy. [...] The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple custom view controllers to manage different portions of the same view hierarchy.

(from the View Controller Programming Guide)

Your sub-controller won't receive rotation events, or viewWillAppear, viewWillDisappear, etc (except viewDidLoad).

So Apple advises us to use a single view controller managing the entire view hierarchy (but doesn't forbid to use multiple ones).

Each view may still be a custom subclass of UIView. Maybe you don't need another view controller but rather a custom view.

Solution 2

[self addSubview:viewControllerB.view];

try this in the sub view

Solution 3

It has always been problematic to simply use addSubview to add a view controller's view as a subview of another's. It's especially bad when people use it to transition between views, rather than relying upon other, more robust, solutions like presentViewController or pushViewController.

If you really want to add one view controller's view as a subview of another's, iOS5 introduced "view controller containment". Containment is discussed in the View Controller Programming Guide as well as WWDC 2011 session 102. Bottom line, you want to ensure you keep your view controller hierarchy synchronized with your view hierarchy, by calls to addChildViewController, didMoveToParentViewController, etc. See the documentation and the video for specifics.

Share:
16,455
nishantcm
Author by

nishantcm

Updated on July 11, 2022

Comments

  • nishantcm
    nishantcm almost 2 years

    Can I load a UiViewController in a UIView in another UIViewcontrller.

    suppose UIViewControllerA has a UIView named subuiview. Can I load UIViewControllerB into subbuiview?

    Thanks!

  • nishantcm
    nishantcm almost 13 years
    UIViewController *myViewControler = [[UIViewController alloc] init]; [someAnotherView addSubview:myViewController.view];
  • nishantcm
    nishantcm almost 13 years
    I am using the above code. If i load anotherviewcontroller in someAnotherView will the first view get deallocated
  • nishantcm
    nishantcm almost 13 years
    How is it possible to do this with only a single view
  • nishantcm
    nishantcm almost 13 years
    Can one viewcontroller have 2 xibs
  • Jilouc
    Jilouc almost 13 years
    @nishantcm Yes it can have 2 xibs. Use initWithNibName:@"Xib1" bundle:nil and initWithNibName:@"Xib2" bundle:nil for instance.
  • nishantcm
    nishantcm almost 13 years
    Can I have a xib for a UIView (not a uiviewcontroller)
  • Jilouc
    Jilouc almost 13 years
    @nishantcm Yes. Take a look at stackoverflow.com/questions/863321/…
  • Daniel
    Daniel over 9 years
    what if you want to show one viewController inside the other?
  • Rob
    Rob over 9 years
    @simpleBob If you want to show one view controller's view inside another view controller's view, then you use "view controller containment" (see the links in my above answer). So, sure, you might do addSubview, but very importantly, you also do a bunch of other stuff to keep the view controller hierarchy synchronized with the view hierarchy (e.g. addChildViewController, etc.). See that WWDC video if you want a nice introduction to the topic and why it's important to do it right.
  • Daniel
    Daniel over 9 years
    yes, I already realized it does not work very if you try it with addSubview directly. Thank you.
  • Jilouc
    Jilouc over 9 years
    @Rob you're absolutely right. I've updated the answer to reflect that.
  • Guig
    Guig over 7 years
    I've found that the sub view controller receives the viewDidAppear, viewWillDisappear ... events. Maybe an addition since 2014
  • Deniss Fedotovs
    Deniss Fedotovs over 7 years
    There should be childViewController.view.frame = ... instead of childViewController.frame = ...