UIButton deinit没被调用

时间:2016-02-23 22:07:53

标签: swift deinit

我有一个自定义的UIButton,想要在我按下之后删除它。但是,当我这样做的时候,某种程度上没有被召唤出去。我在一个空项目中试过这个:

class CustomButton: UIButton {
    deinit { print("deinit") }
}

class ViewController: UIViewController {

    var button: CustomButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        button = CustomButton(frame: CGRectZero)
        button.translatesAutoresizingMaskIntoConstraints = false

        let views = ["button": button]

        view.addSubview(button)

        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-50-[button]", options: [], metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[button]-50-|", options: [], metrics: nil, views: views))

        button.backgroundColor = .redColor()

        button.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)

//      button.removeFromSuperview()
//      button = nil
    }

    func pressed(sender: UIButton) {
        button.removeTarget(nil, action: nil, forControlEvents: .AllEvents)
        button.removeFromSuperview()
        button = nil
    }
}

如果我直接在viewDidLoad()中删除它,将会调用deinit,但每当我使用上面显示的代码时,都不会显示任何内容。怎么叫它叫deinit?这是否意味着它没有被解除分配或仅仅是因为没有被召唤而被解散?

0 个答案:

没有答案