为什么我们必须在调用 addChild(UIViewController) 之后调用 func didMove(toParent parent: UIViewController?)?

Why do we have to call func didMove(toParent parent: UIViewController?) after calling addChild(UIViewController)?

调用 func didMove(toParent parent: UIViewController?) 的确切目的是什么?

苹果文档中说:

If you are implementing your own container view controller, it must call the didMove(toParent:) method of the child view controller after the transition to the new controller is complete or, if there is no transition, immediately after calling the addChild(_:) method.

The removeFromParent() method automatically calls the didMove(toParent:) method of the child view controller after it removes the child.

函数似乎更新了 viewcontroller 的转换,但我无法完全理解调用它背后的想法。如果我不调用它会怎样?

如果您不调用它,子视图控制器将无法检测到它确实移动到了父视图控制器。这可能很重要。有时你需要在移动到父控制器后恰好在子控制器中做一些事情