【发布时间】:2016-12-14 11:39:11
【问题描述】:
UIView 标头表明 superview 属性很强大
open var superview: UIView? { get }
但它的行为就像一个弱属性,即如果我创建 view1 和 view2,然后调用 view1.addSubview(view2),然后只将强 ref 保存到 view2(而不是 view1),即使 view1 也会被取消view2 通过其 superview 属性引用它。
所以,我想知道它在现实中是如何实现的。
编辑:例如,此代码打印“deinit”(ViewController 实例显示在屏幕上),这意味着 view1 已被取消初始化,即使 view2 应该强烈保留它通过superview 属性。
class View: UIView {
deinit {
print("deinit")
}
}
class ViewController: UIViewController {
var view2 = UIView()
override func viewDidLoad() {
super.viewDidLoad()
let view1 = View()
view1.addSubview(view2)
}
}
【问题讨论】:
-
我无法重现该行为。你能发布最少的测试代码吗?
标签: ios xcode uiview uikit superview