iOS通知中心

它是iOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信。通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信。

当通知中心接受到消息后会根据设置,将消息发送给订阅者,这里的订阅者可以有多个

通知中心原理

看完上图你应该明白通知中心所做的事情了吧, 接下来我们就来看看通知中心。

首先必须了解2个类:

// 这个类用来传递发送通知过程中传递信息的载体
NSNotification
// 这是iOS中通知中心的灵魂, 由该类实现了观察者模式, 并给开发者提供了诸如注册、删除观察者的接口, 我们可以通过一个单例来获得它的实例
NSNotificationCenter

代码实现

注册一个通知观察者

// 注册一个通知观察者
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.refreshText(_:)), name: kRefreshTextNotification, object: nil)
self: 观察者
selector: 收到通知执行的方法
name: 通知名
object: // 收到通知后执行的方法
func refreshText(notification: NSNotification) {
if notification.object is NSError {
return
} textField.text = String(notification.object!)
}

向观察者发送通知

NSNotificationCenter.defaultCenter().postNotificationName(kRefreshTextNotification, object: "fffffff", userInfo: nil)
kRefreshTextNotification: 通知名
object: 参数
userInfo: 其他参数

移除观察者

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self, name: kRefreshTextNotification, object: nil)
}

除了上面这种方式系统还有一个

NSNotificationCenter.defaultCenter().addObserverForName(kRefreshTextNotification, object: self, queue: NSOperationQueue.mainQueue()) { (notification) in}

但是在项目中并不多用, 所以就不详细介绍了,其实用法差不多, 只不过用的时候需要注意循环引用问题, 比如你在内部用self, 这个self对应的class对象就不会被释放

移除观察者需要注意 不能用 NSNotificationCenter.defaultCenter().removeObserver(self, name: kRefreshTextNotification, object: nil), 需要用 NSNotificationCenter.defaultCenter().removeObserver(observer)

系统通知

除了自定义的通知名, 系统其实还有一部分通知名

UIDevice通知

UIDeviceOrientationDidChangeNotification    // 设备旋转
UIDeviceBatteryStateDidChangeNotification // 电池状态改变
UIDeviceBatteryLevelDidChangeNotification // 电池电量改变
UIDeviceProximityStateDidChangeNotification // 近距离传感器(比如设备贴近了使用者的脸部)

键盘通知

UIKeyboardWillShowNotification // 键盘即将显示
UIKeyboardDidShowNotification // 键盘显示完毕
UIKeyboardWillHideNotification // 键盘即将隐藏
UIKeyboardDidHideNotification // 键盘隐藏完毕
UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕

系统发出键盘通知时, 会附带一下跟键盘有关的额外信息(字典),字典常见的key如下:

UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame
UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)
UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间
UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)

app通知

// These notifications are sent out after the equivalent delegate message is called
@available(iOS 4.0, *)
public let UIApplicationDidEnterBackgroundNotification: String
@available(iOS 4.0, *)
public let UIApplicationWillEnterForegroundNotification: String
public let UIApplicationDidFinishLaunchingNotification: String
public let UIApplicationDidBecomeActiveNotification: String
public let UIApplicationWillResignActiveNotification: String
public let UIApplicationDidReceiveMemoryWarningNotification: String
public let UIApplicationWillTerminateNotification: String
public let UIApplicationSignificantTimeChangeNotification: String
public let UIApplicationWillChangeStatusBarOrientationNotification: String // userInfo contains NSNumber with new orientation
public let UIApplicationDidChangeStatusBarOrientationNotification: String // userInfo contains NSNumber with old orientation
public let UIApplicationStatusBarOrientationUserInfoKey: String // userInfo dictionary key for status bar orientation
public let UIApplicationWillChangeStatusBarFrameNotification: String // userInfo contains NSValue with new frame
public let UIApplicationDidChangeStatusBarFrameNotification: String // userInfo contains NSValue with old frame
public let UIApplicationStatusBarFrameUserInfoKey: String // userInfo dictionary key for status bar frame
@available(iOS 7.0, *)
public let UIApplicationBackgroundRefreshStatusDidChangeNotification: String
@available(iOS 3.0, *)
public let UIApplicationLaunchOptionsURLKey: String // userInfo contains NSURL with launch URL
@available(iOS 3.0, *)
public let UIApplicationLaunchOptionsSourceApplicationKey: String // userInfo contains NSString with launch app bundle ID
@available(iOS 3.0, *)
public let UIApplicationLaunchOptionsRemoteNotificationKey: String // userInfo contains NSDictionary with payload
@available(iOS 4.0, *)
public let UIApplicationLaunchOptionsLocalNotificationKey: String // userInfo contains a UILocalNotification
@available(iOS 3.2, *)
public let UIApplicationLaunchOptionsAnnotationKey: String // userInfo contains object with annotation property list
@available(iOS 4.0, *)
public let UIApplicationProtectedDataWillBecomeUnavailable: String
@available(iOS 4.0, *)
public let UIApplicationProtectedDataDidBecomeAvailable: String
@available(iOS 4.0, *)
public let UIApplicationLaunchOptionsLocationKey: String // app was launched in response to a CoreLocation event.
@available(iOS 5.0, *)
public let UIApplicationLaunchOptionsNewsstandDownloadsKey: String // userInfo contains an NSArray of NKAssetDownload identifiers
@available(iOS 7.0, *)
public let UIApplicationLaunchOptionsBluetoothCentralsKey: String // userInfo contains an NSArray of CBCentralManager restore identifiers
@available(iOS 7.0, *)
public let UIApplicationLaunchOptionsBluetoothPeripheralsKey: String // userInfo contains an NSArray of CBPeripheralManager restore identifiers
@available(iOS 9.0, *)
public let UIApplicationLaunchOptionsShortcutItemKey: String // userInfo contains the UIApplicationShortcutItem used to launch the app. // Key in options dict passed to application:[will | did]FinishLaunchingWithOptions and info for UIApplicationDidFinishLaunchingNotification
@available(iOS 8.0, *)
public let UIApplicationLaunchOptionsUserActivityDictionaryKey: String // Sub-Dictionary present in launch options when user activity is present
@available(iOS 8.0, *)
public let UIApplicationLaunchOptionsUserActivityTypeKey: String // Key in user activity dictionary for the activity type @available(iOS 8.0, *)
public let UIApplicationOpenSettingsURLString: String // Keys for application:openURL:options:
@available(iOS 9.0, *)
public let UIApplicationOpenURLOptionsSourceApplicationKey: String // value is an NSString containing the bundle ID of the originating application
@available(iOS 9.0, *)
public let UIApplicationOpenURLOptionsAnnotationKey: String // value is a property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property
@available(iOS 9.0, *)
public let UIApplicationOpenURLOptionsOpenInPlaceKey: String // value is a bool NSNumber, set to YES if the file needs to be copied before use // Content size category constants
@available(iOS 7.0, *)
public let UIContentSizeCategoryExtraSmall: String
@available(iOS 7.0, *)
public let UIContentSizeCategorySmall: String
@available(iOS 7.0, *)
public let UIContentSizeCategoryMedium: String
@available(iOS 7.0, *)
public let UIContentSizeCategoryLarge: String
@available(iOS 7.0, *)
public let UIContentSizeCategoryExtraLarge: String
@available(iOS 7.0, *)
public let UIContentSizeCategoryExtraExtraLarge: String
@available(iOS 7.0, *)
public let UIContentSizeCategoryExtraExtraExtraLarge: String // Accessibility sizes
@available(iOS 7.0, *)
public let UIContentSizeCategoryAccessibilityMedium: String
@available(iOS 7.0, *)
public let UIContentSizeCategoryAccessibilityLarge: String
@available(iOS 7.0, *)
public let UIContentSizeCategoryAccessibilityExtraLarge: String
@available(iOS 7.0, *)
public let UIContentSizeCategoryAccessibilityExtraExtraLarge: String
@available(iOS 7.0, *)
public let UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: String // Notification is emitted when the user has changed the preferredContentSizeCategory for the system
@available(iOS 7.0, *)
public let UIContentSizeCategoryDidChangeNotification: String // userInfo dictionary will contain new value for UIContentSizeCategoryNewValueKey
@available(iOS 7.0, *)
public let UIContentSizeCategoryNewValueKey: String // NSString instance with new content size category in userInfo // This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons)
@available(iOS 7.0, *)
public let UIApplicationUserDidTakeScreenshotNotification: String // Extension point identifier constants
@available(iOS 8.0, *)
public let UIApplicationKeyboardExtensionPointIdentifier: String

iOS通知中心的更多相关文章

  1. iOS通知中心升级 -可设置按优先级执行block

    简单介绍下,这是需求驱动中发现iOS的NotificationCenter有很多功能无法实现,于是对其进行了一层包装.相当于手动管理观察者栈和监听者期望执行的事件,因此可以为其添加了很多新增的功能,将 ...

  2. iOS 通知中心 NSNotificationCenter

    iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...

  3. iOS 通知中心扩展制作初步-b

    涉及的 Session 有 Creating Extensions for iOS and OS X, Part 1 Creating Extensions for iOS and OS X, Par ...

  4. QF——iOS通知中心(NotificationCener)

    前面我们讲iOS不同界面间传值的时候,说过可以通过通知中心进行传值.那到底什么是通知中心,他是如何实现传值的呢? NSNotificationCenter是单例的,只提供了一个唯一的实例化入口,在整个 ...

  5. iOS监听模式系列之通知中心

    补充--通知中心 对于很多初学者往往会把iOS中的本地通知.推送通知和iOS通知中心的概念弄混.其实二者之间并没有任何关系,事实上它们都不属于一个框架,前者属于UIKit框架,后者属于Foundati ...

  6. iOS开发之通知中心(NSNotificationCenter)

    前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...

  7. IOS回调机制——代理,通知中心以及Block

    Xcode5.0正式版 IOS7和Xcode5正式版在昨天正式可以下载.IOS7不多说了,交互设计,界面风格,操作的简化程度都属于比较领先的水平. 这里来说说Xcode5正式版,和以前的Xcode5测 ...

  8. IOS Notification 通知中心

    1.     通知中心概述 通知中心实际上是在程序内部提供了消息广播的一种机制.通知中心不能在进程间进行通信.实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象. ...

  9. iOS生命周期 & 通知中心

    笔记内容 学习笔记-段玉磊 Stanford course View Controller Lifecycle 这篇文是我记载Developing iOS 7 Apps公开课 第5课的笔记 UITex ...

随机推荐

  1. BZOJ 1923 外星千足虫(bitset优化线性基)

    题意:给出m次n个千足虫的足数信息,确定在第几次测试后可以确定每个千足虫的来历. 我们可以观察到每个测试结果具有异或后依然成立的性质,于是实际上我们只需要从头到尾确定有n个线性相关的向量是在哪一个测试 ...

  2. 【bzoj1067】[SCOI2007]降雨量 倍增RMQ

    题目描述 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,2004和200 ...

  3. BZOJ1564 NOI2009二叉查找树(区间dp)

    首先按数据值排序,那么连续一段区间的dfs序一定也是连续的. 将权值离散化,设f[i][j][k]为i到j区间内所有点的权值都>=k的最小代价,转移时枚举根考虑是否修改权值即可. #includ ...

  4. 3.3 无连接运输:UDP

    3.3 无连接运输:UDP 简介: UDP提供不可靠的服务,它只做了运输层能做的最少工作,除了分解/复用以及少量的差错检测之外,几乎对IP没增加什么东西. 为什么应用开发人员宁愿再UDP之上构建应用, ...

  5. 【总结】Link-Cut Tree

    这是一篇关于LCT的总结 加删边的好朋友--Link Cut Tree Link-Cut Tree,LCT的全称 可以说是从树剖引出的问题 树剖可以解决静态的修改或查询树的链上信息:那如果图会不断改变 ...

  6. Linux网络接口配置文件解析

    [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0# Intel Corporation 82545EM Gigabit ...

  7. spark streaming (二)

    一.基础核心概念 1.StreamingContext详解 (一) 有两种创建StreamingContext的方式:             val conf = new SparkConf().s ...

  8. mysql数据库----视图、触发器、存储过程、函数、事务、索引、其他语句

    一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT * FROM ( S ...

  9. 一个优质的Vue组件库应该遵循什么样的设计原则

    一.组件库的价值 就个人而言,拥有一套自己的组件库,可以让你的开发变得更高效,让你在行业里更有价值. 就团队而言,拥有一套团队的组件库,可以让协同开发变得更高效规范,让你的团队在公司更具有影响力. 就 ...

  10. Flash平台的分析与RIA的趋势

    10月3号,Flash Player 11 和 AIR 3.0正式提供下载,一片安静.最近这两年来,关于Flash的新闻一向是以负面为主,先是 Silverlight 的挑战,然后是 iphone和i ...