I have a parent viewController that is inserting a childVC with a class method in the childVC. This works great.
class func insertIntoController(parentController: UIViewController, parentView: UIView) -> VideoDoorbellLiveView? {
guard let liveViewController = parentController.storyboard?.instantiateViewControllerWithIdentifier(liveViewIdentifier) as? VideoDoorbellLiveView else { return nil }
liveViewController.view.translatesAutoresizingMaskIntoConstraints = false
parentController.addChildViewController(liveViewController)
parentView.addSubview(liveViewController.view)
let views = ["view": liveViewController.view]
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: views)
constraints.appendContentsOf(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-0-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: views))
parentView.addConstraints(constraints)
liveViewController.didMoveToParentViewController(parentController)
return liveViewController
}
The parent makes sure that a controller is there and sets a couple of String variables, etc. Works beautifully.
guard let controller = VideoDoorbellLiveView.insertIntoController(self, parentView: liveView) else { return }
liveViewController = controller // liveViewController is an optional instance variable
My problem is when the parentVC goes away (the deinit is called), the child VC deinit is not called.
Any ideas why? There is no reference of the parentVC in the childVC.
I even tried to do this in the parentVC without guaranteed success:
deinit {
if let liveViewController = liveViewController {
liveViewController.removeFromParentViewController()
}
}