Home > Blockchain >  what does 【give the child view controller a chance to respond to the change in view ownership】mean?
what does 【give the child view controller a chance to respond to the change in view ownership】mean?

Time:12-27

I know when you add a child view controller to a parent you need to perform the following steps:

  1. 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.

  2. Add the child’s root view to your container’s view hierarchy.

  3. Add any constraints for managing the size and position of the child’s root view.

  4. 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?

refer: https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

CodePudding user response:

There might be some internal system operations that are performed when didMoveToParentViewController is invoked, but I don't if they are documented.

Nevertheless, If you yourself override this method in your own child view controller, this is the place to handle any variable that might have been set in the parent's prepareForSegue.

For example, if in prepareForeSegue you have something like:

(segue.destination as? MyVC).someVar = someValue

The best practice would be reading someVar in MyCV's implementation of didMoveToParentViewController, because this is the point in time when you can be sure that prepareForSegue had already been performed.

CodePudding user response:

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.

  • Related