Understanding the use of addChildViewController

I am working with some code that I need for refactoring. The view controller acts as a container for the other two view controllers and will switch between them, as shown in the code below.

This is not the best design. It may not be necessary to replace the viewing subsystem in this way. I understand it. However, when I work with this code, I want to further understand what happens with the addChildViewController call. I could not find the answer in the Apple docs or related questions (perhaps this is an indication of the need for a design change).

In particular, how does the container controller handle the situation when it is asked to add the child view controller that it has already added? Does he know that he has already added this view controller object?

eg. if the code below is inside the method - and this method is called twice ...

[self addChildViewController:viewControllerB]; [self.view addSubview:viewControllerB.view]; [viewControllerB didMoveToParentViewController:self]; [viewControllerA willMoveToParentViewController:nil]; [viewControllerA.view removeFromSuperview]; [viewControllerA removeFromParentViewController]; 

Thanks Gavin

+8
ios objective-c viewcontroller
source share
1 answer

In general, their recommendations for โ€œcontainingโ€ a view controller when one contains the other should be followed to determine if you need to implement containment.

In particular, worrying about adding the same child view controller twice is to worry about presenting the same view controller twice. If you really thought through everything, you do not have to face this problem. Your guess is correct.

I agree that Apple docs need to be more proactive about what happens to strange parameters or when they fail, but there may also be a case where you donโ€™t want to get attached to fixing errors, which can cause problems along the way. When you develop a design that never calls these methods differently, you solve the problem correctly and donโ€™t depend on any mistake that they may or may not have - more importantly, if you consider this to be a fix Bugs may work differently in the future, disrupting your application.

Walking a little more, you will notice that Apple container view controllers cannot fall into an invalid state (at least not easily with an open API). Using the UITabViewController switching from one view controller to another is an atomic operation, and the tab display controller knows exactly what is happening at any time. The most you need to do is remove the active and show a new one. The only time he blows everything out of the water is when you say "you have to blow everything out of the water and start using these view controllers."

Encoding anything else, such as deleting all views or all view controllers, regardless of what may seem appropriate or reliable in some cases, but this is exactly the opposite, since in fact one end of your code does not trust the other end of yours to keep its part of the deal. In any situation where this actually helps you, it means that you have allowed people to add per-view controllers perforce without the control that you should wish, in which case this is a problem that you need to fix.

+7
source share

All Articles