在使用Swift/Xcode重新打开应用时维护按钮状态,可以通过以下步骤实现:
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
// 其他按钮...
viewDidLoad
方法中,读取保存的按钮状态,并根据需要设置按钮的状态。override func viewDidLoad() {
super.viewDidLoad()
// 读取保存的按钮状态
let button1State = UserDefaults.standard.bool(forKey: "Button1State")
let button2State = UserDefaults.standard.bool(forKey: "Button2State")
// 其他按钮...
// 设置按钮的状态
button1.isSelected = button1State
button2.isSelected = button2State
// 其他按钮...
}
@IBAction func button1Tapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
UserDefaults.standard.set(sender.isSelected, forKey: "Button1State")
}
@IBAction func button2Tapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
UserDefaults.standard.set(sender.isSelected, forKey: "Button2State")
}
// 其他按钮...
这样,当重新打开应用时,按钮的状态将会被正确地恢复。
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云