简体   繁体   中英

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!

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
             
    }
}

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