58

I'm trying to create a container view controller using iOS5 and new methods like addChildViewController.

Do I have to call addSubview after calling addChildViewController?

Do I have to call removeFromSuperview before calling removeChildViewController?

I don't see anything about this in Apple docs. What do you think?

0x8badf00d
  • 6,391
  • 3
  • 35
  • 68
Ricardo
  • 2,831
  • 4
  • 29
  • 42

4 Answers4

84

1) Do I have to call addSubview after calling addChildViewController?

Yes

2) Do I have to call removeFromSuperview before calling removeChildViewController?

Not quite

You should call removeFromParentViewController: instead of removeChildViewController: You should also call willMoveToParentViewController:

For adding / removing, you can refer to this great category :

UIViewController + Container

- (void)containerAddChildViewController:(UIViewController *)childViewController {

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

}

- (void)containerRemoveChildViewController:(UIViewController *)childViewController {

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

}

Official resource at developer.apple.com

Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
  • 1
    Very useful that category. Thanks a lot. – Ricardo Apr 21 '13 at 10:56
  • 1
    @Peter-Lapisu, why don't you call `[childViewController willMoveToParentViewController:self]` in the containerAddChildViewContoller method? – Liron Feb 26 '14 at 06:58
  • 1
    based on this resource, you should not call willMoveToParentViewController:self when INSERTING https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html#//apple_ref/doc/uid/TP40007457-CH18-SW6 – Peter Lapisu Mar 28 '14 at 15:10
  • Regarding "You should also call `willMoveToParentViewController`": note that `willMoveToParentViewController` is automatically called in `addChildViewController`. See the [Discussion section](https://developer.apple.com/reference/uikit/uiviewcontroller/1621381-willmovetoparentviewcontroller) – bunkerdive Mar 10 '17 at 08:43
  • 1
    @bunkerdive thats ok, but it's not called on removeFromParentViewController or removeFromSuperview so you have to call it by yourself when removing – Peter Lapisu Mar 10 '17 at 13:59
43

Short answer: "Yes, and yes." The view hierarchy and the view controller hierarchy are still independent. The containment API simply allows views from other controllers to present themselves within a parent controller's view in a clean and consistent way.

You can find a bit in Apple's docs here... this is a relevant passage from the "Container View Controllers Arrange Content of Other View Controllers" section:

A container manages a view hierarchy just as other view controllers do. A container can also add the views of any of its children into its view hierarchy. The container decides when such a view is added and how it should be sized to fit the container’s view hierarchy, but otherwise the child view controller remains responsible for the view and its subviews.

If you have access, I would highly recommend checking out the WWDC 2011 video entitled "Implementing UIViewController Containment" (download it from Apple Developer Video Archive).

Cœur
  • 37,241
  • 25
  • 195
  • 267
macserv
  • 3,546
  • 1
  • 26
  • 36
  • 1
    Thanks a lot. If I destroy the container, do I have also to call removeViewFromSuperview? Or even removeChildViewController? – Ricardo Apr 13 '12 at 22:00
  • Your quote of Apple's doc can only be found on archive.org. You may want to replace it with a newer quote from [1](https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html), [2](https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/) or [3](https://developer.apple.com/documentation/uikit/uiviewcontroller). – Cœur Jun 09 '19 at 10:40
3

Adding to Peter's answer: one reason I found for calling addChildViewController before addSubview was that when addSubview is called then the viewDidLoad of the child get's called, and in some cases you will want to have the parent-child hierarchy properly set up at that point. If that isn't done, during child's the viewDidLoad the parentViewController property will be nil.

user3099609
  • 2,318
  • 18
  • 20
0

The below is an example provided by Apple documentation.

- (void) displayContentController: (UIViewController*) content {
   [self addChildViewController:content];
   content.view.frame = [self frameForContentController];
   [self.view addSubview:self.currentClientView];
   [content didMoveToParentViewController:self];
}

You can also go through the detailed explanation given here - https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

This will give you idea about child and parent view controller relations and how to work with them.

Avinash B
  • 1,485
  • 13
  • 14