我正在尝试保存单选按钮的状态,但是它对我没有任何想法该怎么做?我现在正在使用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()
}
}
答案 0 :(得分:1)
不要使用相同的键来存储两个按钮的状态。并使用相同的键来存储和检索按钮的状态。
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")
}
}
}
答案 1 :(得分:0)
似乎您的代码显然嵌套不正确。例如,以下代码实际上并未在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()
}
}
由于这些代码行不在该函数的右括号之内,因此这些行实际上并未包含在任何函数中,因此将永远不会被调用。