-
Notifications
You must be signed in to change notification settings - Fork 472
Adding and Removing Child View Controllers
Charlie Hieger edited this page Oct 13, 2016
·
2 revisions
Adding and removing child view controllers ensures that the parent view controller knows of its children. This will help when doing things like calling a modal from a child view that has been added to the parent. This behavior can be buggy if the parent doesn't know that it's connected to the child. Another reason is so that viewWillAppear
, viewDidAppear
, viewWillDisappear
, and viewDidDisappear
work consistently.
addChildViewController(yourViewController)
view.addSubview(yourViewController.view)
yourViewController.didMove(toParentViewController: self)
func displayContentController(content: UIViewController) {
addChildViewController(content)
self.view.addSubview(content.view)
content.didMove(toParentViewController: self)
}
yourViewController.willMove(toParentViewController: nil)
yourViewController.view.removeFromSuperview()
yourViewController.removeFromParentViewController()
func hideContentController(content: UIViewController) {
content.willMove(toParentViewController: nil)
content.view.removeFromSuperview()
content.removeFromParentViewController()
}