关于willMoveToParentViewController方法和didMoveToParentViewController方法的使用:
这两个方法用在childVC交换的时候调用!
当调用willMoveToParentViewController方法或didMoveToParentViewController方法时,要注意他们的参数使用。
当我们调用[parentVC addChildViewController:childVC]
,将childVC加入到parentVC时,需要额外调用[childVC didMoveToParentViewController:parentVC];
,此时参数传入parentVC。无需在addChild之前调用willMoveToParentViewController
方法。因为在addChild调用时,默认会调用willMoveToParent方法,而不会调用didMoveToParent方法。
当我们调用[childVC removeFromParentViewController]
,将自身从parentVC移除时,需要额外调用[childVC willMoveToParentViewController:nil]
,此时参数传nil。无需调用[childVC didMoveToParentViewController:parentVC]
方法。因为在调用removeFromParent方法时,默认会调用didMoveToParentViewController方法。
addChildViewController:
不会自动把childVC的view添加到parentVC的view上,需要调用[parentVC.view addSubview:childVC.view]
,此时才会调用childVC的viewWillAppear等生命周期方法。
同理,removeFromParentViewController
也不会自动把childVC的view从parentVC的view中移除,需要手动调用[childVC.view removeFromSuperview]
,从而调用到childVC的viewWillDisap等周明周期方法