iosswifteureka-forms

How can I enable/disable my modal when the row value of my switch row changes?


I want to activate/deactivate my modal depending on the current value of my switch row.

I have a SettingsViewController where the user can enable or disable it:

class SettingsFormViewController : FormViewController, MFMailComposeViewControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        form 
            +++ Section("Messages")
            <<< SwitchRow("message_users") { row in
                row.title = "Activate messages"
            }.onChange { row in
                row.title = (row.value ?? false) ? "Deactivate messages" : "Activate messages"
                row.updateCell()
            }
            

Now in my ReloadManager I want to check if the row is enabled or not. If it is enabled a modal should show up and if not it shouldn't:


class ReloadManager {

...

private func showModalFromSettings() {

             let nav = UINavigationController()
             let ctrl = MessageFormViewController()
                 
             ctrl.preferredContentSize = CGSize(width: 600, height: 400)
             nav.pushViewController(ctrl, animated: true)
             nav.modalPresentationStyle = .popover
               
             UIApplication.shared.keyWindow?.rootViewController!.present(nav, animated: true, completion: nil)
             nav.popoverPresentationController?.sourceView = UIApplication.shared.keyWindow?.rootViewController?.view
             
    }
}

What would be the best approach to check if the row is enabled or not and then passing the value to my ReloadManager ? Thanks in advance!


Solution

  • You can user UserDefaults to save status of the row.

     +++ Section("Messages")
                    <<< SwitchRow("message_users") { 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: "RowStatus")
                    }
    
    private func showModalFromSettings() {
                 let rowStatus = UserDefaults.standard.bool(forKey: "RowStatus")
                 if rowStatus {
                      //Do something when row enabled
                 } else {
                     //Do something when row disabled
                 }
                 let nav = UINavigationController()
                 let ctrl = MessageFormViewController()
                     
                 ctrl.preferredContentSize = CGSize(width: 600, height: 400)
                 nav.pushViewController(ctrl, animated: true)
                 nav.modalPresentationStyle = .popover
                   
                 UIApplication.shared.keyWindow?.rootViewController!.present(nav, animated: true, completion: nil)
                 nav.popoverPresentationController?.sourceView = UIApplication.shared.keyWindow?.rootViewController?.view
                 
        }
    }