如何保存按钮的开关值?我有一个开关按钮,当用户打开应用程序并单击关闭按钮时,我希望应用程序将其保存,以便重新打开应用程序时,该按钮的值仍为关闭。希望这可以通过使用User Defaults
来完成。我的代码:Swift - 如何保存图像按钮?
class ViewController: UIViewController {
var soundEnable:Bool = true
var soundImage:UIImage?
@IBAction func soundbtn(_ sender: UIButton) {
let soundButton = sender
if (soundEnable) {
soundImage = UIImage.init(named: "mute.png")
soundEnable = false
} else {
soundImage = UIImage.init(named: "sound.png")
soundEnable = true
}
soundButton.setImage(soundImage, for: UIControlState.normal)
}
}
你想要什么时候关闭应用程序,然后你回来,那么它应该显示选定的图像?或者你想要改变点击行动? –
@Dharmbir Singh我希望它显示选定的图像。谢谢! –
lly然后看到我编辑的答案。 –