【问题标题】:Memory keep increasing with custom view内存随着自定义视图不断增加
【发布时间】:2015-03-10 07:57:13
【问题描述】:


请看我的代码

protocol CustomViewDelegate: class {

}
class CustomView: UIView {
    var button: UIButton! = {
        //setup
        }()
    weak var delegate: CustomViewDelegate?
}

class controller: UIViewController {
    var customView: CustomView! = {
        //setup
    }()

    private func setup() {
        customView.button.addTarget(self, action: "actionForButton:", forControlEvents: .TouchUpInside)
    }
}

起初,我认为这段代码不会造成任何问题,因为我已经为委托设置了weak。此外,我已经这样编码一年多了,它工作得很好。

但我错了,这行代码确实会导致内存增加,这让我很惊讶。

为什么这会在 swift 中发生,它在 Objective-C 中运行良好?
有没有更好的解决方案?

谢谢。

【问题讨论】:

    标签: ios swift memory-management automatic-ref-counting


    【解决方案1】:

    方法addSubview(_:) (developer.apple.com) 建立了强引用,因此您的var button: UIButton!var customView: CustomView! 也应该是weak

    【讨论】:

      【解决方案2】:

      也许您多次将其添加为子视图。

      【讨论】:

        猜你喜欢
        • 2015-07-09
        • 1970-01-01
        • 1970-01-01
        • 2013-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-05
        • 1970-01-01
        相关资源
        最近更新 更多