簡體   English   中英

當我的開關行的行值更改時,如何啟用/禁用我的模式?

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

我想根據我的開關行的當前值激活/停用我的模式。

我有一個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()
            }
            

現在在我的ReloadManager 中,我想檢查該行是否已啟用。 如果啟用,則應顯示模態,否則不應顯示:


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

檢查行是否已啟用,然后將值傳遞給我的ReloadManager的最佳方法是什么? 提前致謝!

您可以使用 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