我已经阅读了很多有关保留周期的信息。必要时,父母UIViewController
应始终strong
引用其子女,UIViewController
而孩子应weak
引用其父母。
只有在他们互相参照时才这样吗?例如,如果父级UIViewController
没有对其子级的引用,那么子级可以对strong
其父级进行引用UIViewController
吗?我可以避免这种情况吗,还是从长远来看会出现这种内存问题的坏习惯?
来自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 }
和
// An array of children view controllers. This array does not include any presented view controllers.
@available(iOS 5.0, *)
public var childViewControllers: [UIViewController] { get }
在这里,您可以看到parent和child之间已经存在 strong
和weak
引用ViewControllers
。您不应strong
在从子级到父级的方向上添加更多新引用,因为这可能导致内存泄漏。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句