创建主开关以更改所有背景颜色

Create master switch to change all background colors

我想创建一个主开关,当它关闭时,它会将每个视图控制器的背景颜色 class 更改为红色。我不知道您是否可以创建一个函数来调用持有开关的 class。开关将采用不同的 class,但如有必要,背景也可以更改为红色。


    import UIKit
    class Switch: UIViewController {
    var toggle = UISwitch()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(toggle)
        //set frame and other properties...

        toggle.addTarget(self, action: #selector(toggleWasToggled(_:)), for: .allEvents)
          toggle.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate ([

            toggle.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :175),
            toggle.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 100),
            toggle.widthAnchor.constraint(equalToConstant: 350),
            toggle.heightAnchor.constraint(equalToConstant: 180),
            ])
    }

    @objc func toggleWasToggled(_ sender: UISwitch) {
        //Whenever the switch is toggled you can post a notification. You can even post two seperate notifications, one for when the toggle is on, and one for when it is off.
        NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "ColorChange"), object: nil, userInfo: nil))

    }
    @objc func colorWasChanged(_ sender: Any) {
        view.backgroundColor = UIColor.blue
        print("espn")
    }
    }

    class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //Add an observer in all your viewControllers that need to be notified of the color change.
        NotificationCenter.default.addObserver(self, selector: #selector(colorWasChanged(_:)), name: Notification.Name(rawValue: "ColorChange"), object: nil)
    }


    @objc func colorWasChanged(_ sender: Any) {
    view.backgroundColor = UIColor.blue
        print("cnn")
    }
    }

您可以将通知用于类似这样的事情。这是一个例子:

Swift 5.0

class Switch: UIViewController {
    var toggle = UISwitch()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(toggle)
        //set frame and other properties...

        toggle.addTarget(self, action: #selector(toggleWasToggled(_:)), for: .allEvents)
    }

    @objc func toggleWasToggled(_ sender: UISwitch) {
        //Whenever the switch is toggled you can post a notification. You can even post two seperate notifications, one for when the toggle is on, and one for when it is off.
        NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "ColorChange"), object: nil, userInfo: nil))
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //Add an observer in all your viewControllers that need to be notified of the color change.
        NotificationCenter.default.addObserver(self, selector: #selector(colorWasChanged(_:)), name: Notification.Name(rawValue: "ColorChange"), object: nil)
    }

    //its important to also remove the observer when the viewController is no longer being displayed
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        NotificationCenter.default.removeObserver(self)
    }

    @objc func colorWasChanged(_ sender: Any) {
        //Change color to red here
    }
}

创建一个 BaseViewController class 并在其中添加观察者并更改颜色 viewcontroller。

class BaseViewController: UIViewController {

    static var switchStatus = false

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(colorChanged(_:)), name: Notification.Name(rawValue: "ColorChanged"), object: nil)
    }
    @objc func colorChanged(_ notification: Notification) {
        if let switchStatus = notification.userInfo?["switchStatus"] as? Bool {
            BaseViewController.switchStatus = switchStatus
            if switchStatus {
                self.view.backgroundColor = .red
            } else {
                self.view.backgroundColor = .white
            }
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if BaseViewController.switchStatus {
            self.view.backgroundColor = .red
        } else {
            self.view.backgroundColor = .white
        }
    }
}

所有其他 viewcontroller 都继承自此 viewcontroller。无需在每个 class 中添加观察者。因为它是从 BaseViewController 继承的,所以它会自动添加一个观察者来改变颜色。

class ViewController: BaseViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

现在,当您更改 UISwitch 中的值时 post 会收到一个包含 UISwitch 状态的通知。开关class也是继承自BaseViewController

class Switch: BaseViewController {
    var toggle = UISwitch()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(toggle)
        //set frame and other properties...
        toggle.addTarget(self, action: #selector(toggleWasToggled(_:)), for: .allEvents)
    }
    @objc func toggleWasToggled(_ sender: UISwitch) {
        let userInfo = ["switchStatus":sender.isOn]
        NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "ColorChanged"), object: nil, userInfo: userInfo))
    }
}