如何在Swift中解决_ContiguousArrayStorage <uibutton>内存泄漏?

时间:2017-04-17 15:17:27

标签: ios swift memory-leaks uibutton

我是Leak Instrument的新手,并开始学习如何修复内存泄漏。我有以下代码:

class InputButtonsContainerView: UIView {

var hexButtons = [UIButton]()

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    //Configure buttons
    for view in subviews {
        for btn in view.subviews where btn is UIButton {
            let button = btn as! UIButton
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor.black.cgColor
            button.layer.cornerRadius = SizeAdaptation.shared.inputButtonCornerRadius
            button.titleLabel?.font = UIFont.systemFont(ofSize: SizeAdaptation.shared.inputButtonFontSize)
            if button.tag > 9 && button.tag < 16 {
                hexButtons.append(button)
            }
        }
    }
    defer{
        inputMode = .dec
    }
}

var inputMode = MakerInputMode.dec {
    didSet{
        switch inputMode {
        case .dec:
            for btn in hexButtons {
                btn.layer.borderColor = UIColor.lightGray.cgColor
                btn.setTitleColor(UIColor.lightGray, for: .normal)
            }
        case .hex:
            for btn in hexButtons {
                btn.layer.borderColor = UIColor.black.cgColor
                btn.setTitleColor(UIColor.black, for: .normal)
            }
        }
    }
}
}

在AddColorViewController类中,有一个InputButtonsContainerView的IBOutlet对象:

class AddColorViewController: UIViewController {
    @IBOutlet weak var inputButtonsContainerView: InputButtonsContainerView!
    ....
}

泄漏仪器显示init功能有泄漏:    enter image description here    enter image description here    enter image description here 似乎UIButton数组被释放两次并导致引用计数为-1。有谁知道如何解决这一问题 ?谢谢。

0 个答案:

没有答案