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

标签 ios uiviewcontroller uikit

调用func didMove(toParentparent: 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.

看起来该函数更新了 View Controller 的转换,但我无法完全理解调用它背后的想法。如果我不调用它会发生什么?

最佳答案

如果您不调用它, subview Controller 将无法检测到它确实移动到父 View Controller 。这可能很重要。有时您需要在移动到父 Controller 后立即在子 Controller 中执行某些操作

关于ios - 为什么我们必须在调用 addChild(UIViewController) 之后调用 func didMove(toParent Parent : UIViewController? ) ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57001422/

相关文章:

ios - 禁用内部时嵌套 UITableViews : a way to scroll only the outer UITableView,?

ios - 从后台线程在 UINavigationItem 上显示 UISearchController

ios - IOS 示例中的自定义 UUID 对 BLE 意味着什么?

ios - Root View Controller Swift

ios - 在 iOS 中用新的 Storyboard 替换当前的 Storyboard

iphone - sizeWithFont : method 的替代方案

ios - UIView过渡动画

ios - 切换子viewController使用分段控制

iphone - 警告 : Attempt to present <AdjustPhotoViewController> on <TakePhotoViewController> while a presentation is in progress

Iphone GUI 物理代码