简体   繁体   中英

iOS - Child View Controller Having “Strong” Reference To Parent View Controller

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM