我希望创建一个主开关,当它关闭时,它会将每个视图控制器类的背景颜色更改为红色。我不知道你是否可以创建一个函数来调用保存开关的类。该开关将处于不同的类别,但如果需要,背景也可以更改为红色。
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")
}
}
创建一个BaseViewController类并在此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继承的所有其他viewcontrollers。无需在每个类中添加观察者。由于它是从BaseViewController继承的,因此会自动添加一个用于更改颜色的观察者。
class ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
现在,当您更改UISwitch中的值时,发布具有UISwitch状态的通知。 Switch类也继承自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))
}
}
你可以使用Notifications这样的东西。这是一个例子:
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
}
}