iOS - 子视图控制器对父视图控制器有“强引用”

4
我已经阅读了很多关于保留周期的文章。当必要时,父级UIViewController应该始终具有对其子级UIViewControllerstrong引用,而子级应该对其父级具有weak引用。
这只是在它们相互引用时吗?例如,如果父级UIViewController没有任何对其子级的引用,那么子级可以对其父级UIViewController具有strong引用吗?我能行得通吗,还是这是长期存在内存问题的不良实践?
1个回答

5

来自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 }

在这里,你可以看到父子ViewControllers之间已经有了强引用(strong references)弱引用(weak references)的关系。但是,你应该避免从子控制器到父控制器方向添加更多新的强引用,以免导致内存泄漏。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接