iOS通知中心
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通知中心的更多相关文章
- iOS通知中心升级 -可设置按优先级执行block
简单介绍下,这是需求驱动中发现iOS的NotificationCenter有很多功能无法实现,于是对其进行了一层包装.相当于手动管理观察者栈和监听者期望执行的事件,因此可以为其添加了很多新增的功能,将 ...
- iOS 通知中心 NSNotificationCenter
iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...
- iOS 通知中心扩展制作初步-b
涉及的 Session 有 Creating Extensions for iOS and OS X, Part 1 Creating Extensions for iOS and OS X, Par ...
- QF——iOS通知中心(NotificationCener)
前面我们讲iOS不同界面间传值的时候,说过可以通过通知中心进行传值.那到底什么是通知中心,他是如何实现传值的呢? NSNotificationCenter是单例的,只提供了一个唯一的实例化入口,在整个 ...
- iOS监听模式系列之通知中心
补充--通知中心 对于很多初学者往往会把iOS中的本地通知.推送通知和iOS通知中心的概念弄混.其实二者之间并没有任何关系,事实上它们都不属于一个框架,前者属于UIKit框架,后者属于Foundati ...
- iOS开发之通知中心(NSNotificationCenter)
前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...
- IOS回调机制——代理,通知中心以及Block
Xcode5.0正式版 IOS7和Xcode5正式版在昨天正式可以下载.IOS7不多说了,交互设计,界面风格,操作的简化程度都属于比较领先的水平. 这里来说说Xcode5正式版,和以前的Xcode5测 ...
- IOS Notification 通知中心
1. 通知中心概述 通知中心实际上是在程序内部提供了消息广播的一种机制.通知中心不能在进程间进行通信.实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象. ...
- iOS生命周期 & 通知中心
笔记内容 学习笔记-段玉磊 Stanford course View Controller Lifecycle 这篇文是我记载Developing iOS 7 Apps公开课 第5课的笔记 UITex ...
随机推荐
- wp开发(二)--获取用户篇
本文从个人开发者的角度来谈如何让wp应用获得尽可能多的用户.当然前提是你的app有一定的竞争性,如果不具备竞争力,那再多的用户下载也是白扯,所以最关键的还是要保证app的质量. 一. 程序图标 千万不 ...
- 两个list比较相等元素
1.实现方式 public class list { public static void main(String[] args) { List<String> list1 = new A ...
- 【HLSDK系列】怎么增加一种新实体
你平常肯定接触到很多比如 info_player_start hostage info_target 之类的实体,这里就解释一下怎么创建一种新的实体. 首先建立一个新的 .h 文件(当然你写在现有的文 ...
- BZOJ4881 线段游戏(二分图+树状数组/动态规划+线段树)
相当于将线段划分成两个集合使集合内线段不相交,并且可以发现线段相交等价于逆序对.也即要将原序列划分成两个单增序列.由dilworth定理,如果存在长度>=3的单减子序列,无解,可以先判掉. 这个 ...
- 【ListBox】ListBox的相关操作
Winform中两个listbox的操作是平时比较常用的操作. 本次将以一个Winform实例来分享一下两个listbox的操作,包括:listbox添加项,项的上移下移等操作. 假设有两个listb ...
- 【BZOJ3667】Rabin-Miller算法(Pollard_rho)
[BZOJ3667]Rabin-Miller算法(Pollard_rho) 题面 呜,权限题,别问我是怎么做的(我肯定没有权限号啊) 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一 ...
- 基于 Quartz.NET 实现可中断的任务
基于 Quartz.NET 实现可中断的任务 Quartz.NET 是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET 允许开发 ...
- Codeforces Round #338 (Div. 2) D 数学
D. Multipliers time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 97.B Superset
A set of points on a plane is called good, if for any two points at least one of the three condition ...
- poj1659 Frogs' Neighborhood
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10239 Accepted: 4 ...