Swift iOS : NotificationCenter

栏目: Swift · 发布时间: 6年前

内容简介:Swift iOS : NotificationCenter

类NotificationCenter提供了一种轻耦合的消息传递机制。可以发起一个通知,在多处监听此通知。比如说一个App的主题样式被修改,就可以通过此类来通知多个相关UI,做响应的处理。

如下案例展示了这种可能:

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做了一处对此通知的监听。然后当:

NotificationCenter.default.post(name: Notification.Name("themeChange"), object: nil)

执行时,三处监听函数都会被调用。

NotificationCenter还可以监听系统通知,比如App进入前景和背景,按如下方法监听即可:

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按钮,可以看到输出:

applicationDidEnterBackground

再度执行App可以看到输出:

applicationWillEnterForeground

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

风吹江南之互联网金融

风吹江南之互联网金融

陈宇(江南愤青) / 东方出版社 / 2014-6-1 / 55元

随着中国互联网金融浪潮高涨,P2P、众筹、余额宝、微信支付等新生事物层出不穷,加之大数据等时髦概念助阵,简直是乱花渐欲迷人眼,令媒体兴奋,公众狂热。那么,互联网金融真的能“颠覆”传统金融吗?当互联网思维对撞传统金融观念,是互联网金融的一统天下,还是传统金融业的自我革新?究竟是谁动了金融业的奶酪? 本书作者早期试水创立具有互联网金融雏形的网站,后来成为互联网金融的资深投资人,基于其多年在该领域......一起来看看 《风吹江南之互联网金融》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具