When implementing UIViewController containment, what is the correct order of the following messages I have to send, when exchanging one child controller with another?
Is it as below, or is my order incorrect? Am I missing messages?
addChildViewController
:addSubview
:didMoveToParentViewController
:removeFromSuperview
:removeFromParentViewController
:EDIT: I have to annotate that the above only becomes an issue of you are not using UIViewController's transition methods but rather want to add the new view manually.
Okay, figured it out. It's all in the docs but I find it pretty well hidden. I'll split it up into three cases because I think it might help others. Why am I making it so difficult instead of using [UIViewController transitionFromViewController:toViewController:duration:options:animations:completion]
? The answer is that you can only use the transition method if there is already an existing view controller. If you want to transition from "no controller" to some controller or vice versa, the above method will throw an exception.
Case 1: Both controllers are equal - that includes that both are null
Case 2: The old controller is NULL, and the new controller is not NULL. Just add the new controller.
addChildViewController
to new controller - this will trigger an implicit willMoveToParentViewController
addSubview
didMoveToParentViewController
Case 3: old controller is not NULL and new controller is not NULL. Run a transition between controllers.
addChildViewController
to new controller - this will trigger an implicit willMoveToParentViewController
willMoveToParentViewController
and pass it nil as the new parentUIView's
animations.didMoveToParentViewController
to the new controller.removeFromParentViewController
to the old controller - this will trigger an implicit didMoveToParentViewController
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With