Difference between addChildViewController and addSubview?
Asked Answered
Q

7

18

Both the methods add the view as child of parentview and view can receive events. When to use which one?

Quillon answered 25/5, 2015 at 12:53 Comment(3)
hi please refer this link. #11187258Roethke
Here are some good answers -> #17192505Horsa
addchildviewcontroller works for ViewControllers( adding it as a sub- controller of a mainViewcontroller especially in containerViewControllers) and the other works addSubview on views (adding subviews on a parentView). The basic difference of their action is on ViewController and UIView respectively. Read this #17192505Bromberg
L
12

They are very different. addChildViewController associates a view controller with a parent container view controller, while addSubview adds a view to the view hierarchy of the view it is being added to. In the former case, the new child view controller will be responsible for handling events when it is the selected view controller of its parent. Think of a tab bar controller--each tab has its own associated "child" view controller that displays its view within the parent tab bar controller's content area and handles any user interaction within that view when its corresponding tab is selected in the tab bar. You should only use addChildViewController when you have a custom container view and want to add a new view controller to its childViewControllers property. If you just want to add a new view to the view hierarchy that can receive events, which is what it kind of sounds like, addSubview is the way to go. "Implementing a Container View Controller" section explains what addChildViewController is for.

Lethbridge answered 25/5, 2015 at 13:14 Comment(0)
M
27

It all depends on how you want to manage the new subview. If you want the new subview to be managed by the current view's view controller (e.g. you're adding something simple like a few UILabel objects), you simply call addSubview. If, on the other hand, the new subview has its own view controller (i.e. it's sufficiently complicated collection of views, with rich functionality, that you want to encapsulate all of this complexity with its own controller to manage everything this new subview does) then you call addChildViewController to add the new view controller, but then call addSubview, too.

So, note that addChildViewController, itself, does nothing with the views. You generally immediately follow it with calls that add its view, too, e.g. here is a slightly clarified example from the Implementing a Custom Container View Controller section of the View Controller Programming Guide for iOS:

[self addChildViewController:childViewController];        // add subview's view controller
childViewController.view.frame = ...                      // specify where you want the new subview
[self.view addSubview:childViewController.view];          // now you can add the child view controller's view
[childViewController didMoveToParentViewController:self]; // now tell the child view controller that the adding of it and its views is all done

So, it's not a question of addSubview vs addChildViewController, but rather addSubview vs addChildViewController+addSubview. If you call addChildViewController, you're doing so with the intent of calling addSubview for its view at some point.

Frankly, this question of addSubview vs. addChildViewController+addSubview is rarely how we think about this. A more logical way of thinking of this is determine whether this new view has its own view controller. If it does, you perform the addChildViewController sequence of calls. If not, you just call addSubview.

For a good introduction to view controller containment (e.g. the rationale for that API, the importance of keeping the view hierarchy synchronized with the view controller hierarchy, etc.), see WWDC 2011 video Implementing UIViewController Containment.

Millard answered 26/5, 2015 at 12:55 Comment(4)
In the code block. I understand all lines except for the last line. It seems redundant. You have already told the parentViewController that it has a childView. Why do you have to tell the childView that you have a parent?! When adding a subview we just do self.addSubView(subview). We don't add an extra subview.didGerAddedToSuperViewOccupant
It is not redundant. It is essential part of containment. I'd suggest you want that video, which describes why you need this. Bottom line, willMoveToParentViewController (which is called by addChildViewController) and didMoveToParentViewController bookend the process (like viewWillAppear and viewDidAppear).Millard
Oh, OK, nice analogy. want that video <-- what do you mean? What video?Occupant
WWDC 2011 Implementng UIViewController Containment.Millard
L
12

They are very different. addChildViewController associates a view controller with a parent container view controller, while addSubview adds a view to the view hierarchy of the view it is being added to. In the former case, the new child view controller will be responsible for handling events when it is the selected view controller of its parent. Think of a tab bar controller--each tab has its own associated "child" view controller that displays its view within the parent tab bar controller's content area and handles any user interaction within that view when its corresponding tab is selected in the tab bar. You should only use addChildViewController when you have a custom container view and want to add a new view controller to its childViewControllers property. If you just want to add a new view to the view hierarchy that can receive events, which is what it kind of sounds like, addSubview is the way to go. "Implementing a Container View Controller" section explains what addChildViewController is for.

Lethbridge answered 25/5, 2015 at 13:14 Comment(0)
H
3

addChildViewController is method in UIViewController class and addSubview is in UIView class

Both have entirely different behavior.

addChildViewController just puts a view controller in front of the current one. You have to manage the flow of controllers. This method is only intended to be called by an implementation of a custom container view controller.

addSubview adds another view as sub view to the view of that object.

Haberman answered 25/5, 2015 at 13:16 Comment(0)
O
2

Knowing that MVC means Model-View-Controller:

If you only intend to add the view, then use addSubview. e.g. adding a label, button.

However if you intend to a d view + controller then you must use addChildViewController to add its controller and ALSO addSubView to add its view. e.g. adding another viewController, tableViewController.

In addition:

There are two categories of events that are forwarded to child view controllers:

1- Appearance Methods:

- viewWillAppear:
- viewDidAppear:
- viewWillDisappear:
- viewDidDisappear:

2- Rotation Methods:

- willRotateToInterfaceOrientation:duration:
- willAnimateRotationToInterfaceOrientation:duration:
- didRotateFromInterfaceOrientation:

An example of where you would run into an issue if you don't do such is here

For more information I strongly recommend to see answers on this question.

Occupant answered 4/1, 2017 at 15:16 Comment(0)
M
1

Available from iOS 5, the addChildViewController:

- (void)addChildViewController:(UIViewController *)childController NS_AVAILABLE_IOS(5_0);

method let you add any view controller as a child to some other view controller, but first it removes any parent from the childController and than add it as a child view controller to the specified controller.

The child controller is nothing but an instance of UIViewController and thus it will provide the functionality of view controller (i.e it will receive the events like -(void)viewWillAppear, -(void)viewWillDisappear, etc as a normal UIViewController does).

On the other hand

 - (void)addSubview:(UIView *)view;

addSubview: will add any view as subview on any other view.

It's not the choice which to use when rather it's the type which asks to use specific method.

For Instance -

If you have an instance of UIViewController than you will definitely use addChildViewController: (also you can use presentModalViewController, pushViewController) and if you have an instance of UIView than definitely you have to use addSubview.

Note : You can also add view controller's view as a subview to any other view as well.

Maunsell answered 25/5, 2015 at 13:15 Comment(0)
T
1

Based on some test, I found that: If child view controller is not added to parent view controller (supposing the parent view controller is under the root view controller), only child view controller's view is added to parent view controller's view, then:

  • the sub view controller can still receive messages directly related to view, such as - viewWillAppear:, - viewWillLayoutSubviews, etc.

But

  • the sub view can not receive some system messages, such as - willRotateToInterfaceOrientation:duration:

I can't give the messages list now, however.

Trouper answered 14/3, 2016 at 7:41 Comment(0)
P
0

addChildViewController is used to prevent the added sub view controller from releasing, in other word, the parent view controller will hold a strong reference to the sub view controller.

Performing answered 8/4, 2016 at 9:46 Comment(1)
Your post is more like a suggestionMemoir

© 2022 - 2024 — McMap. All rights reserved.