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];
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 .
source share