Ios 随着自定义视图的增加,内存不断增加
Ios 随着自定义视图的增加,内存不断增加,ios,swift,memory-management,automatic-ref-counting,Ios,Swift,Memory Management,Automatic Ref Counting,请看一下我的代码 protocol CustomViewDelegate: class { } class CustomView: UIView { var button: UIButton! = { //setup }() weak var delegate: CustomViewDelegate? } class controller: UIViewController { var customView: CustomView! =
请看一下我的代码
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中运行良好?有没有更好的解决办法
谢谢。也许您多次将其添加为子视图 方法
addSubview(:)
()建立了一个强引用,因此您的var按钮:ui按钮代码>和var customView:customView代码>也应该是弱的