I made a SwitchRow with Eureka but can't get it to work that my switch saves it current state. It is always shown as off when the SettingsView is presented modally. It is a little bit irritating because I save the row value to the UserDefaults and you can't tell if it is on or not. Here is my code:
class SettingsView : FormViewController{
override func viewDidLoad() {
super.viewDidLoad()
+++ Section("Messages")
<<< SwitchRow("message_people_withId") { row in
row.title = "Activate messages"
}.onChange { row in
row.title = (row.value ?? false) ? "Deactivate messages" : "Activate messages"
row.updateCell()
UserDefaults.standard.set(row.value ?? false, forKey: "MessageSettingsRow")
}
This creates a switch which changes the title if the user turns it off or on. But the ui changes are not reflected. I thought the best way would be to check for the switch row itself and change its value depending on the row value:
if row.value == true {
(self.form.rowBy(tag: "message_people_withId") as? SwitchRow)?.cell.switchControl.isOn = true
} else {
(self.form.rowBy(tag: "message_people_withId") as? SwitchRow)?.cell.switchControl.isOn = false
}
but this changes nothing.
Any suggestions? Thanks in advance.
You need to define the row.value
Therefore making your code similar to;
class SettingsView : FormViewController{
override func viewDidLoad() {
super.viewDidLoad()
+++ Section("Messages")
<<< SwitchRow("message_people_withId") { row in
row.title = "Activate messages"
row.value = UserDefaults.standard.bool(forKey: "MessageSettingsRow")
}.onChange { row in
row.title = (row.value ?? false) ? "Deactivate messages" : "Activate messages"
row.updateCell()
UserDefaults.standard.set(row.value ?? false, forKey: "MessageSettingsRow")
}
This will ensure the correct value on viewDidLoad()
.
Your second code block, the one that evalutes row.value
to be true or not will always return false given the standard state of the SwitchRow
is false.