I know when you add a child view controller to a parent you need to perform the following steps:
Call the addChildViewController:
method of your container view controller.
This method tells UIKit that your container view controller is now managing the view of the child view controller.
Add the child’s root view to your container’s view hierarchy.
Add any constraints for managing the size and position of the child’s root view.
Call the didMoveToParentViewController:
method of the child view controller.
The document explains that you need to call didMoveToParentViewController:
to 「give the child view controller a chance to respond to the change in view ownership.」,Does anyone know what this means and what exactly didMoveToParentViewController:
does?
didMoveToParentViewController
is just like many other UIViewController
lifetime methods such as:
viewDidLoad
viewWillAppear
or various UIView
methods such as:
didMoveToSuperView
willMoveToWindow
Your own subclasses may or may not have a need to perform specific actions during these events. But the methods exist just incase you do.
So the statement "give the child view controller a chance to respond to the change in view ownership." simply means that if your view controller needs to perform some logic based on the fact that it just became a child view controller of a container view controller, then you can override didMoveToParentViewController
and perform whatever logic you need.
UIViewController
may or may not perform its own logic in that method but that's an implementation you don't need to worry about.
You are being told that your container view controller must call didMoveToParentViewController
so you must call it. But you only need to override didMoveToParentViewController
in your child view controller if your child view controller needs to perform something specific in that case.