Reputation: 1922
I've read a lot about retain cycles. When necessary, a parent UIViewController
should always have a strong
reference to its child UIViewController
while the child should have a weak
reference to its parent.
Is this ONLY when they're referencing each other? For example, if the parent UIViewController
DOES NOT have any reference to its child, can the child have a strong
reference to its parent UIViewController
? Can I get away with this, or is this bad practice in terms of memory issues coming up in the long run?
Upvotes: 4
Views: 1882
Reputation: 146
From UIViewController.h
.
/*
If this view controller is a child of a containing view controller (e.g. a navigation controller or tab bar
controller,) this is the containing view controller. Note that as of 5.0 this no longer will return the
presenting view controller.
*/
weak public var parentViewController: UIViewController? { get }
and
// An array of children view controllers. This array does not include any presented view controllers.
@available(iOS 5.0, *)
public var childViewControllers: [UIViewController] { get }
Here you can see, that there are already strong
and weak
references between parent and child ViewControllers
. You should not add any more new strong
references in the direction from child to parent as it can lead to memory leaks.
Upvotes: 5