如何保存单选按钮的状态?
Posted
技术标签:
【中文标题】如何保存单选按钮的状态?【英文标题】:how to save the state of radio button? 【发布时间】:2019-04-25 04:52:32 【问题描述】:我正在尝试保存我的单选按钮的状态,但它对我不起作用,知道该怎么做吗?我现在正在使用 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()
【问题讨论】:
UserDefaults
使用了 3 个不同的密钥,并且不一致。您正在尝试对两个不同的按钮使用相同的键。并且不要使用synchronize()
,它已被弃用。
为什么要用同一个键来存储两个不同按钮的状态?
我是新手,请告诉我该怎么做。我会很感激你的帮助
【参考方案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")
【讨论】:
【参考方案2】:看来您的代码显然嵌套不正确。例如,以下代码实际上并未在 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()
因为这些代码行位于该函数的右括号之外,所以这些代码行实际上并未包含在任何函数中,因此永远不会被调用。
【讨论】:
以上是关于如何保存单选按钮的状态?的主要内容,如果未能解决你的问题,请参考以下文章