I have 1 Parent Controller and 1 View Controller. I have successfully performed the addChildViewController and transitionFromViewController functions to leverage the Containment API provided by iPhone SDK. Yet before I can actually pass view controllers to those functions I have a few questions
Question 1: In the ParentVC ViewDidLoad method must I instantiate all 4 child view controllers using the following
UIViewController* child = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
OR self.myViewController = [[MyViewController alloc] init];
Question 2: Assuming I do not instantiate all 4 child views initially in my ViewDidLoad… each time I add a new child using addChildViewController before I perform a transition using transitionFromViewController, am I required to instantiate the child VC I am desiring to add?
Question 3: Under the condition that the answers to Question 1 and/or Question 2 are ‘Yes’ what must be done about memory management. For Question 1, when navigating back to the parent VC from a child VC, it seems to mean that a new objects of the 4 child view controllers will be created, correct? When looking at Question 2, when transitions are made, which can be often, it seems to create a new copy of the child view controller objects each time. In essence this app could become very bloated after some transitions between parent and child, and between child and child views.
Question 4: When transitionFromViewController is being called, what is happening in memory? What happens to the parent VC in a parent-child transition? What happens to the child VC in a child-child transition?
UPDATE: The following quote from this particular tutorial seems to refer to some of my concerns (emphasis added):
Note that
self.storyboardisn’t set from aninitcall. At the moment,
there isn’t a great place to put this initialization code. One option
is to overridesetStoryboard:and another isviewDidLoad. The problem
with both options is that you should guard theaddChildViewController:
call to make sure that View Controllers aren’t instantiated twice.
No, you don’t need to instantiate all the children in the custom container view controller’s
viewDidLoadif you don’t want. You can if you want, but you don’t have to. You can instantiate them in a just-in-time manner if you would prefer.Not only would you need to instantiate before
transitionFromViewController, but obviously beforeaddChildController, too.If you choose to instantiate all of the view controllers up front, what member objects are created by the individual controllers is a function of how you handle it in those controllers, so it’s impossible to answer that in the abstract. But this might be academic now that you know you don’t have to instantiate all of the potential children at once.
transitionFromViewControllerwill not affect the memory consumed by the controllers. What controls that would be the instantiating of the controller (and adding it toaddChildViewController) and the subsequentremoveFromParentViewController(assuming that’s the last strong reference you have to the controller) that you might do in conjunction withtransitionFromViewController.