内容简介: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python基础教程(第2版)
Magnus Lie Hetland / 司维、曾军崴、谭颖华 / 人民邮电出版社 / 2010-7 / 69.00元
本书是经典教程的全新改版,作者根据Python 3.0版本的种种变化,全面改写了书中内容,做到既能“瞻前”也能“顾后”。本书层次鲜明、结构严谨、内容翔实,特别是在最后几章,作者将前面讲述的内容应用到了10个引人入胜的项目中,并以模板的形式介绍了项目的开发过程。本书既适合初学者夯实基础,又能帮助Python程序员提升技能,即使是 Python方面的技术专家,也能从书里找到令你耳目一新的东西。一起来看看 《Python基础教程(第2版)》 这本书的介绍吧!