简体   繁体   English

当我的开关行的行值更改时,如何启用/禁用我的模式?

[英]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:我有一个SettingsViewController ,用户可以在其中启用或禁用它:

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.现在在我的ReloadManager 中,我想检查该行是否已启用。 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 ?检查行是否已启用,然后将值传递给我的ReloadManager的最佳方法是什么? Thanks in advance!提前致谢!

You can user UserDefaults to save status of the row.您可以使用 UserDefaults 来保存行的状态。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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