Skip to content

Instantly share code, notes, and snippets.

@xgjames
Forked from tomohisa/gist:2897676
Last active August 29, 2015 14:25
Show Gist options
  • Save xgjames/7391bd7469289a413bf1 to your computer and use it in GitHub Desktop.
Save xgjames/7391bd7469289a413bf1 to your computer and use it in GitHub Desktop.
Add and Remove ChildViewController
// add child view
UIViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:@"test"];
[self addChildViewController:controller];
controller.view.frame = CGRectMake(0, 44, 320, 320);
[self.view addSubview:controller.view];
[controller didMoveToParentViewController:self];
// remove child view
UIViewController *vc = [self.childViewControllers lastObject];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
@xgjames
Copy link
Author

xgjames commented Jul 22, 2015

You should call [vc willMoveToParentViewController:nil]; before removeFromSuperview;

UIViewController *vc = [self.childViewControllers lastObject];
[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];

https://gist.github.com/tomohisa/2897676#gistcomment-1209661

@xgjames
Copy link
Author

xgjames commented Jul 22, 2015

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