Who owns a child view controller?
Who owns a child view controller?
- Subject: Who owns a child view controller?
- From: Jeremy Hughes <email@hidden>
- Date: Wed, 12 Jul 2017 17:34:19 +0100
I’m trying to understand who owns a child view controller after it has been
removed from its parent view controller.
The following code (which doesn’t involve view controllers) behaves as I would
expect:
import Cocoa
class Element
{
var children = [Element]()
weak var parent: Element?
func addChild(_ child: Element)
{
children.append(child)
child.parent = self
}
}
let parent = Element()
var child: Element? = Element()
weak var childReference: Element? = child
parent.addChild(child!)
child = nil
parent.children = []
if childReference == nil
{
print("OK")
}
else
{
print(“Why is childReference not nil?”
}
// Prints “OK"
But if I replace Element with NSViewController, it behaves differently:
import Cocoa
let parent = NSViewController()
var child: NSViewController? = NSViewController()
weak var childReference: NSViewController? = child
parent.addChildViewController(child!)
child = nil
parent.childViewControllers = []
if childReference == nil
{
print("childReference is nil")
}
else
{
print("Why is childReference not nil?")
}
// Prints "Why is childReference not nil?”
Jeremy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden