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 protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to