类NotificationCenter提供了一种轻耦合的消息传递机制。能够发起一个通知,在多处监听此通知。好比说一个App的主题样式被修改,就能够经过此类来通知多个相关UI,作响应的处理。app
以下案例展现了这种可能:ide
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow();
self.window?.frame=UIScreen.main.bounds;
self.window?.makeKeyAndVisible();
NotificationCenter.default.addObserver(self, selector: #selector(themeChange), name: Notification.Name("themeChange"), object: nil)
self.window?.rootViewController = Page()
return true
}
func themeChange(){
print("themeChange2")
}
}
class Page: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
NotificationCenter.default.addObserver(self, selector: #selector(themeChange), name: Notification.Name("themeChange"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(themeChange1), name: Notification.Name("themeChange"), object: nil)
NotificationCenter.default.post(name: Notification.Name("themeChange"), object: nil)
}
func themeChange(){
print("themeChange")
}
func themeChange1(){
print("themeChange1")
}
}复制代码
执行此代码,输出应该是:函数
themeChange2
themeChange
themeChange1复制代码
经过 NotificationCenter.default.addObserver在类Page1作了两处对“themeChange”通知的监听,在类AppDelegate作了一处对此通知的监听。而后当:post
NotificationCenter.default.post(name: Notification.Name("themeChange"), object: nil)复制代码
执行时,三处监听函数都会被调用。
NotificationCenter还能够监听系统通知,好比App进入前景和背景,按以下方法监听便可:spa
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow();
self.window?.frame=UIScreen.main.bounds;
self.window?.makeKeyAndVisible();
self.window?.rootViewController = Page()
return true
}
}
class Page: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
}
func applicationWillEnterForeground(){
print("applicationWillEnterForeground")
}
func applicationDidEnterBackground(){
print("applicationDidEnterBackground")
}
}复制代码
应用执行后,按HOME按钮,能够看到输出:code
applicationDidEnterBackground复制代码
再度执行App能够看到输出:server
applicationWillEnterForeground复制代码
能够传递和接受对象做为参数,像是这样传递:对象
let cd = {(_ a : String) in print(a)}
NotificationCenter.default.post(name: Notification.Name("dive2"), object: [1,"2",cd])复制代码
像是这样接收:it
func dive2(_ obj : Any){
nav?.pushViewController(Level2(), animated: true)
print(obj)
// {name = dive2; object = (
// 1,
// 2,
// "(Function)"
// )}
}复制代码