類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)"
// )}
}複製代碼