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

以上所述就是小编给大家介绍的《Swift iOS : NotificationCenter》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

程序设计语言

程序设计语言

斯科特 / 裘宗燕 / 电子工业出版社 / 2005-1 / 88.00元

这是一本很有特色的教材,其核心是讨论程序设计语言的工作原理和技术。本书融合了传统的程序设计语言教科书和编译教科书的有关知识,并增加了一些有关汇编层体系结构的材料,以满足没学过计算机组织的学生们的需要。书中通过各种语言的例子,阐释了程序设计语言的重要基础概念,讨论了各种概念之间的关系,解释了语言中许多结构的形成和发展过程,以及它们演化为今天这种形式的根源。书中还详细讨论了编译器的工作方式和工作过程,......一起来看看 《程序设计语言》 这本书的介绍吧!

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

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器