简体   繁体   中英

Why is my Switch always off when the controller is presented?

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.

Making it simple your row value is not getting updated

 print(row.value)

You are getting every time true so your switch always on

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
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM