我希望创建一个主开关,当它关闭时,它将每个视图控制器类的背景颜色更改为红色。不知道能不能创建一个函数来调用持有开关的类。开关将在不同的类中,但如果需要,背景也可以更改为红色。
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 类并在此视图控制器中添加观察者并更改颜色。
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
}
}
}
对于从该视图控制器继承的所有其他视图控制器。无需在每个类中添加观察者。因为它是从 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))
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句