如何保存按鈕的開關值?我有一個開關按鈕,當用戶打開應用程序並單擊關閉按鈕時,我希望應用程序將其保存,以便重新打開應用程序時,該按鈕的值仍爲關閉。希望這可以通過使用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然後看到我編輯的答案。 –