Swift iOS : NotificationCenter

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

内容简介: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

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

查看所有标签

猜你喜欢:

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

PHP与MySQL权威指南

PHP与MySQL权威指南

吴津津、田睿、李云、刘昊 / 机械工业出版社华章公司 / 2011-10 / 118.00元

PHPChina官方出品,Discuz!创始人戴志康、UCHome创始人李国德、ThinkPHP创始人刘晨、PHPCMS项目负责人王参加等联袂推荐。 本书是目前为止最全面的关于PHP与MySQL开发技术的书籍之一,系统而全面地讲解了PHP与MySQL技术的方方面面,适合初中级的PHP程序员系统地学习;本书也是目前为止首本系统而深入地讲解UCenter、Discuz!、UCHome、ShopN......一起来看看 《PHP与MySQL权威指南》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具