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 ...
随机推荐
- BZOJ4919 大根堆(动态规划+treap+启发式合并)
一个显然的dp是设f[i][j]为i子树内权值<=j时的答案,则f[i][j]=Σf[son][j],f[i][a[i]]++,f[i][a[i]+1~n]对其取max.这样是可以线段树合并的, ...
- 【CodeChef PREFIXOR】Prefix XOR
https://odzkskevi.qnssl.com/f0fbdb108ec813b1294f8f714805963b?v=1502083692 网上搜到的题解: http://blog.csdn. ...
- 使用adb录制手机屏幕视频
adb shell screenrecord命令可以用来录制Android手机视频 screenrecord是一个shell命令,支持Android4.4(API level 19)以上,支持视频格式 ...
- form, table表示表格的时候有什么区别?
http://zhidao.baidu.com/link?url=1DFrMJlzV_fHSyGmKEi77ki6g2IrjrMfRGwVYNHL5Y8iJC9Diu2BoMGEiB3wbnkTCHm ...
- 随机抽样一致性算法(RANSAC)转载
这两天看<计算机视觉中的多视图几何>人都看蒙了,转载一些干货看看 转自王先荣 http://www.cnblogs.com/xrwang/archive/2011/03/09/ransac ...
- 学习 opencv---(11)OpenC 边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器
本篇文章中,我们将一起学习OpenCV中边缘检测的各种算子和滤波器——Canny算子,Sobel算子,Laplace算子以及Scharr滤波器.文章中包含了五个浅墨为大家准备的详细注释的博文配套源代码 ...
- git 删除远程仓库的命令
# 删除远程仓库的命令: git branch -r -d origin/branch-name #其中这条命令必须执行,远程仓库才会删除 git push origin :branch-name # ...
- sloop公共函数之添加信号,定时器及socket
1:添加信号 1.1 原型:sloop_handle sloop_register_signal(int sig, sloop_signal_handler handler, void * param ...
- pandans导出Excel并将数据保存到不同的Sheet表中
数据存在mongodb中,按照类别导出到Excel文件,问题是想把同一类的数据放到一个sheet表中,最后只导出到一个excel文件中# coding=utf-8import pandas as pd ...
- jquery ajax thinkphp异步局部刷新完整流程
环境:ThinkPHP3.2.3,jQuery3.2 前言: 在一般的网站中,都需要用到jquery或者其他框架(比如angular)来处理前后端数据交互,thinkphp在后台也内置了一些函数用 ...