如何使用userdefaults保存切换按钮的状态?这是代码,请帮助我。
import UIKit
class LawFigure: UIViewController {
//Outlets:
@IBOutlet weak var likeButton: UIButton!
//Variables:
var isButtonOn = false
override func viewDidLoad() {
super.viewDidLoad()
customiseUI()
}
func customiseUI() {
likeButton.layer.borderWidth = 3.0
likeButton.layer.borderColor = UIColor(red: 0/255, green: 166/255, blue: 221/255, alpha: 1.0).cgColor
likeButton.layer.cornerRadius = 20.0
likeButton.clipsToBounds = true
}
@IBAction func followActionPressed(_ sender: Any) {
self.activateButton(bool: !isButtonOn)
}
func activateButton(bool: Bool) {
isButtonOn = bool
likeButton.backgroundColor = bool ? UIColor(red: 0/255, green: 166/255, blue: 221/255, alpha: 1.0) : .clear
likeButton.setTitle(bool ? "unlike": "like", for: .normal)
likeButton.setTitleColor(bool ? .white : UIColor(red: 0/255, green: 166/255, blue: 221/255, alpha: 1.0), for: .normal)
}
}
最佳答案
您可以尝试打开所有应用程序
likeButton.isEnabled = !(UserDefaults.standard.bool(forKey:"IsBtnEnabled"))
每当单击按钮保存
UserDefaults.standard.set(state, forKey: "IsBtnEnabled")
关于ios - 如何使用xCode Swift中的用户默认值永久存储数据以切换按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49436574/