Difference between addChildViewController and addSubview?

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

+6
source share
7 answers

They are very different. addChildViewController associates the view controller with the parent container view controller, while addSubview adds the view to the view hierarchy to which it is added. In the first case, the new child view controller will be responsible for handling the 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, which displays its view in the content area of ​​the parent tab bar controller and handles any user interaction in that view when the 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 hierarchy of views that can receive events, which you think addSubview is the way to go. The "Implementing a Container Controller" section explains what addChildViewController is.

+6
source

It all depends on how you want to manage the new view. If you want the new subordinate to be controlled using the current view controller (for example, you are adding something simple, for example several UILabel objects), you simply call addSubview . If, on the other hand, the new subview has its own view controller (i.e., it is a fairly complex set of views with rich functionality that you want to encapsulate all this complexity with your own controller to manage all this new sub-task), then you call addChildViewController to add a new view controller, but then also call addSubview .

So, note that addChildViewController , by itself, does nothing with views. Usually you just follow it with calls that also add its presentation, for example. here is a little clarified example from Implementing a custom container controller in the View Controller Programming Guide for iOS:

 [self addChildViewController:childViewController]; // add subview 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 view [childViewController didMoveToParentViewController:self]; // now tell the child view controller that the adding of it and its views is all done 

So this is not a question of addSubview vs addChildViewController , but rather addSubview vs addChildViewController + addSubview . If you call addChildViewController , you do this with the intention of calling addSubview for your view at some point.

Honestly, this question is about addSubview vs. addChildViewController + addSubview rarely recalls this. A more logical way to think about this is to determine if this new view has its own view controller. If so, you are executing a sequence of calls to addChildViewController . If not, you just call addSubview .

For a good familiarization with the controller's view (for example, the rationale for this API, the importance of maintaining the view hierarchy is synchronized with the view controller hierarchy, etc.) see WWDC 2011 video Implementing the UIViewController Containment .

+13
source

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

Both have completely different behavior.

addChildViewController simply sets the view controller in front of the current one. You must control the flow of controllers. This method is intended only to invoke the implementation of the user controller of the container view.

addSubview adds another sub-view to the view of this object.

+2
source

Knowing that MVC stands for Model-View-Controller:

If you intend to add a view , use addSubview . e.g. add tag, button.

However, if you intend to use the d view + controller , you must use addChildViewController to add your controller and ALSO addSubview to add its view. for example, adding another viewController, tableViewController.

Besides:

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

1- Appearance Methods:

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

2- Rotation Methods:

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

For more information, I highly recommend seeing the answers to this question.

+2
source

Available from iOS 5, addChildViewController:

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

allows you to add any view controller as a child to any other view controller, but first it removes any parent from the childController and adds it as a child view controller to the specified controller.

A child controller is nothing more than an instance of a UIViewController and thus it will provide the functionality of a view controller (i.e. it will receive events such as -(void)viewWillAppear , - (void)viewWillDisappear , etc., as regular UIViewController ).

On the other hand,

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

addSubview: will add any view as a subview to any other view.

This is not a choice that should be used when it is rather a type that asks to use a particular method.

For instance -

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

Note You can also add a view manager view as a subview to any other view.

+1
source

Based on some test, I found that: if the child view controller is not added to the parent view controller (if the parent view controller is under the root view controller), only the child view controller view is added to the parent view controller view:

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

But

  • under viewing it cannot receive system messages, for example - willRotateToInterfaceOrientation:duration:

However, I cannot provide a list of messages.

0
source

addChildViewController is used to prevent the addition of an added view controller, in other words, the parent view controller will contain a strong reference to the sub-view controller.

0
source

Source: https://habr.com/ru/post/987861/


All Articles