Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct order of UIViewController containment messages I have to send when not using transitionFromViewController?

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?

  • Add new child controller to current controller: addChildViewController:
  • Add subview of new child controller to current controller's view: addSubview:
  • Run some fancy transition from old view to new view.
  • Inform new child controller that is was added to another controller: didMoveToParentViewController:
  • Remove the previous controller's view from its superview: removeFromSuperview:
  • Remove the previous child controller from its parent: 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.

like image 910
Krumelur Avatar asked Mar 29 '12 13:03

Krumelur


1 Answers

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

  • Do nothing, we already have what we want on screen. :-)

Case 2: The old controller is NULL, and the new controller is not NULL. Just add the new controller.

  • Send addChildViewController to new controller - this will trigger an implicit willMoveToParentViewController
  • Make the new view the size you want
  • Insert the child controller's view into this controller's view hierarchy: addSubview
  • Inform child controller that it was added as a child by sending it: didMoveToParentViewController

Case 3: old controller is not NULL and new controller is not NULL. Run a transition between controllers.

  • Adjust new view's frame/bounds.
  • Send addChildViewController to new controller - this will trigger an implicit willMoveToParentViewController
  • Send the old controller willMoveToParentViewController and pass it nil as the new parent
  • Add the new subview to your view
  • Run your custom transition between the old and the new view using UIView's animations.
  • In the animation end delegate, send didMoveToParentViewController to the new controller.
  • Remove the view of the old controller from its superview.
  • Send removeFromParentViewController to the old controller - this will trigger an implicit didMoveToParentViewController
like image 106
Krumelur Avatar answered Nov 06 '22 11:11

Krumelur