我试图保存我的单选按钮的状态,但它不适合我任何想法如何做到这一点?我现在正在使用userdefaults
class WTSettingsVC: UIViewController {
@IBOutlet weak var checkBttn: DLRadioButton!
@IBOutlet weak var checkBttn2: DLRadioButton!
var switchON : Bool = false
var button : Bool = false
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
checkBttn.isSelected = UserDefaults.standard.bool(forKey: "issaved")
checkBttn2.isSelected = UserDefaults.standard.bool(forKey: "issaved")
}
override func viewWillAppear(_ animated: Bool) {
super .viewWillAppear(animated)
self.setCommonNavigationBar(title: "Settings", largeTitle: true, tranpernt: false, tint: WTConstant.LMColors.greenTheme, fontColor: .white)
checkBttn?.isEnabled = UserDefaults.standard.bool(forKey: "switchState")
checkBttn?.addTarget(self, action: #selector(bttnAction(_:)), for: .touchUpInside)
}
@IBAction func bttnAction(_ sender: DLRadioButton) {
if sender.tag == 1 {
sender.isEnabled = true
switchON = true
defaults.set(switchON, forKey: "switchON")
}else if sender.tag == 2{
sender.isEnabled = true
switchON = false
defaults.set(switchON, forKey: "switchON")
}
}
}
UserDefaults.standard.set(sender.isEnabled, forKey: "switchState")
UserDefaults.standard.synchronize()
}
}
不要使用相同的键来存储两个按钮的状态。并使用相同的键来存储和检索按钮的状态。
class WTSettingsVC: UIViewController {
@IBOutlet weak var checkBttn: DLRadioButton!
@IBOutlet weak var checkBttn2: DLRadioButton!
override func viewDidLoad() {
super.viewDidLoad()
checkBttn.isSelected = UserDefaults.standard.bool(forKey: "checkBttnSelected")
checkBttn2.isSelected = UserDefaults.standard.bool(forKey: "checkBttn2Selected")
checkBttn.addTarget(self, action: #selector(bttnAction(_:)), for: .touchUpInside)
checkBttn2.addTarget(self, action: #selector(bttnAction(_:)), for: .touchUpInside)
}
@IBAction func bttnAction(_ sender: DLRadioButton) {
if sender == checkBttn {
UserDefaults.standard.set(true, forKey: "checkBttnSelected")
UserDefaults.standard.set(false, forKey: "checkBttn2Selected")
}else if sender == checkBttn2 {
UserDefaults.standard.set(false, forKey: "checkBttnSelected")
UserDefaults.standard.set(true, forKey: "checkBttn2Selected")
}
}
}
看起来您的代码显然是嵌套不当。例如,以下代码实际上没有在bttnAction函数中执行:
UserDefaults.standard.set(sender.isEnabled, forKey: "switchState")
UserDefaults.standard.synchronize()
此代码需要放在它上面的大括号内。尝试重写您的代码,类似于:
class WTSettingsVC: UIViewController {
@IBOutlet weak var checkBttn: DLRadioButton!
@IBOutlet weak var checkBttn2: DLRadioButton!
var switchON : Bool = false
var button : Bool = false
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
checkBttn.isSelected = UserDefaults.standard.bool(forKey: "issaved")
checkBttn2.isSelected = UserDefaults.standard.bool(forKey: "issaved")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.setCommonNavigationBar(title: "Settings", largeTitle: true, tranpernt: false, tint: WTConstant.LMColors.greenTheme, fontColor: .white)
checkBttn?.isEnabled = UserDefaults.standard.bool(forKey: "switchState")
checkBttn?.addTarget(self, action: #selector(bttnAction(_:)), for: .touchUpInside)
}
@IBAction func bttnAction(_ sender: DLRadioButton) {
if sender.tag == 1 {
sender.isEnabled = true
switchON = true
defaults.set(switchON, forKey: "switchON")
} else if sender.tag == 2 {
sender.isEnabled = true
switchON = false
defaults.set(switchON, forKey: "switchON")
}
UserDefaults.standard.set(sender.isEnabled, forKey: "switchState")
UserDefaults.standard.synchronize()
}
}
因为这些代码行在该函数的结束括号之外,所以这些行实际上并未包含在任何函数中,因此永远不会被调用。