按钮不会解除分配

时间:2014-10-15 22:03:48

标签: ios iphone button swift

var ExitImg: UIImage!
var ExitButton: UIButton!

func missileHitAction(sender:UIButton!)
{
    self.view.viewWithTag(12221)?.removeFromSuperview()
    ExitImg = nil
    ExitButton = nil
}
override func viewDidLoad() {
super.viewDidLoad()
ExitImg = UIImage(contentsOfFile: "/Users/Joca/Desktop/Game_dev/missile_gun1")
    ExitButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
    ExitButton.frame = CGRectMake(5, 285, 70, 30)
    ExitButton.imageView?.tag = 12221
    ExitButton.setBackgroundImage(ExitImg, forState: UIControlState.Normal)
    ExitButton.addTarget(self, action: "missileHitAction:", forControlEvents: UIControlEvents.TouchUpInside)

    self.view.addSubview(ExitButton)

}

这是一个取消分配按钮的示例测试项目,按钮按下按钮应该被释放,但不是:(

1 个答案:

答案 0 :(得分:0)

您正在创建按钮,但未设置其标记。相反,您正在设置其图像视图的标记。而不是:

ExitButton.imageView?.tag = 12221

我认为你打算:

ExitButton.tag = 12221