我试图修复其他错误,所以它可能看起来有点不同,但这是我能做的最好的。
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning()
Box49.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
Box49.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)
Box50.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
Box50.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)
Box51.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
Box51.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)
}
@IBAction func Box49(_ sender: UIButton) {
sender.isSelected = !(sender as AnyObject).isSelected
}
@IBAction func Box50(_ sender: UIButton) {
sender.isSelected = !(sender as AnyObject).isSelected
}
@IBAction func Box51(_ sender: UIButton) {
sender.isSelected = !(sender as AnyObject).isSelected
}
答案 0 :(得分:2)
首先,您需要创建3个按钮插座,因为您当前正在将图像设置为按钮操作,因此要么在界面构建器中设置选定图像和普通图像,要么创建3按钮插座,如下所示。
@IBOutlet var box49: UIButton!
@IBOutlet var box50: UIButton!
@IBOutlet var box51: UIButton!
现在将图像设置为viewDidLoad中的Button或viewDidAppear
中不在didReceiveMemoryWarning
中的按钮,也不需要将发件人转发到AnyObject
以访问isSelected
属性,因为发件人类型是UIButton
,因此您可以直接访问isSelected
属性。
var BoxOFF = #imageLiteral(resourceName: "UnCheckBox")
var BoxON = #imageLiteral(resourceName: "CheckBox")
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
box49.setImage(BoxOFF, for: .normal)
box49.setImage(BoxON, for: .selected)
box50.setImage(BoxOFF, for: .normal)
box50.setImage(BoxON, for: .selected)
box51.setImage(BoxOFF, for: .normal)
box51.setImage(BoxON, for: .selected)
}
现在以这种方式设置你的按钮动作。
@IBAction func Box49(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
@IBAction func Box50(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
@IBAction func Box51(_ sender: Button) {
sender.isSelected = !sender.isSelected
}
答案 1 :(得分:1)
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
当内存不足而需要释放任何资源时,会调用上述方法。
因此,您需要将上述方法中的代码添加到viewDidAppear
或viewWillAppear
。
所以替换如下代码:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
Box49.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
Box49.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)
Box50.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
Box50.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)
Box51.setImage(#imageLiteral(resourceName: "UnCheckBox"), for: .normal)
Box51.setImage(#imageLiteral(resourceName: "CheckBox"), for: .selected)
}
另一项改进是替换您的代码
sender.isSelected = !(sender as AnyObject).isSelected
带
sender.isSelected = !sender.isSelected