Skip to content

Instantly share code, notes, and snippets.

@andrewsardone
Last active December 18, 2015 22:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewsardone/5854342 to your computer and use it in GitHub Desktop.
Save andrewsardone/5854342 to your computer and use it in GitHub Desktop.

To form a parent-child relationship:

  1. Send the -addChildViewController: message to the parent with the child as the parameter. This will send the -willMoveToParentViewController: message to the child with the parent as the parameter.
  2. Add the child view controller's view as a subview to the parent view controller's view.
  3. Send the -didMoveToParentViewController: message to the child with the parent as the parameter.

For example,

[parent addChildViewController:child];
[parent.view addSubview:child.view];
[child didMoveToParentViewController:parent]

To break a parent-child relationship, simply send the -removeFromParentViewController message to the child after sending -willMoveToParentViewController:.

[child willMoveToParentViewController:nil];
[child.view removeFromSuperview];
[child removeFromParentViewController];

To transition between child view controllers, say, for creating your own UINavigationController-like object:

  1. Make sure the destination view controller has been added as a child via the -addChildViewController: message – transitioning between two view controllers requires both children to have the same parent.
  2. Send the -transitionFromViewController:toViewController:duration:options:animations:completion: message to the parent with your from and to view controllers. This will handle adding and remove the view controllers' views from the parent's view hierarchy.
  3. Within the completion block, send the -removeFromParentViewController message to the from view controller and the -didMoveToParentViewController: message to the to view controller with the parent as the parameter.

For example,

[parent addChildViewController:toVC];

[parent transitionFromViewController:fromVC
                                toVC:toVC
                            duration:0.0
                             options:UIViewAnimationOptionTransitionNone
                          animations:nil
                          completion:^(BOOL finished) {
                              [fromVC willMoveToParentViewController:nil];
                              [fromVC removeFromParentViewController];
                              [toVC didMoveToParentViewController:parent];
                          }];
@lukeredpath
Copy link

You need to call willMoveToParentViewController:nil when breaking a parent child relationship.

@andrewsardone
Copy link
Author

Ah, yes – you're right

Similarly removeFromParentViewController: does not call [self willMoveToParentViewController:nil] before removing the child.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment