Swift - 使用NSNotificationCenter发送通知,接收通知
来源:互联网 发布:光纤三端口环形器原理 编辑:程序博客网 时间:2024/05/21 14:52
标签:
1,通知(NSNotification)介绍
这里所说的通知不是指发给用户看的通知消息,而是系统内部进行消息传递的通知。要介绍通知之前,我们需要先了解什么是观察者模式。在 MVC 里,观察者模式意味着需要允许 Model 对象和 View 对象进行交流,而不能有直接的关联。
2,系统通知的注册和响应
比如我们想要在用户按下设备的home键,程序进入后台时执行某些操作。一种办法是在AppDelegate.swift里的applicationDidEnterBackground方法里执行。
除此之外,由于程序进入后台会发送 UIApplicationDidEnterBackgroundNotification 的通知,我们可以事先注册个监听这个通知的“观察者”来处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import
UIKit
class
ViewController
:
UIViewController
{
override
func
viewDidLoad() {
super
.viewDidLoad()
let
notificationCenter =
NSNotificationCenter
.defaultCenter()
let
operationQueue =
NSOperationQueue
.mainQueue()
let
applicationDidEnterBackgroundObserver =
notificationCenter.addObserverForName(
UIApplicationDidEnterBackgroundNotification
,
object:
nil
, queue: operationQueue, usingBlock: {
(notification:
NSNotification
!)
in
print
(
"程序进入到后台了"
)
})
//如果不需要的话,记得把相应的通知注册给取消,避免内存浪费或奔溃
//notificationCenter.removeObserver(applicationDidEnterBackgroundObserver)
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
3,使用自定义的通知
通知类型其实就是一个字符串,所以我们也可以使用自己定义的通知(同时也可以传递用户自定义数据)。
下面创建了两个观察者获取下载图片通知,同时收到通知后的处理函数内部添加了个3秒的等待。
--- ViewController.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import
UIKit
class
ViewController
:
UIViewController
{
let
observers = [
MyObserver
(name:
"观察器1"
),
MyObserver
(name:
"观察器2"
)]
override
func
viewDidLoad() {
super
.viewDidLoad()
print
(
"发送通知"
)
NSNotificationCenter
.defaultCenter().postNotificationName(
"DownloadImageNotification"
,
object:
self
, userInfo: [
"value1"
:
"hangge.com"
,
"value2"
: 12345])
print
(
"通知完毕"
)
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
--- MyObserver.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import
UIKit
class
MyObserver
:
NSObject
{
var
name:
String
=
""
init
(name:
String
){
super
.
init
()
self
.name = name
NSNotificationCenter
.defaultCenter().addObserver(
self
, selector:
"downloadImage:"
,
name:
"DownloadImageNotification"
, object:
nil
)
}
func
downloadImage(notification:
NSNotification
) {
let
userInfo = notification.userInfo
as
! [
String
:
AnyObject
]
let
value1 = userInfo[
"value1"
]
as
!
String
let
value2 = userInfo[
"value2"
]
as
!
Int
print
(
"\(name) 获取到通知,用户数据是[\(value1),\(value2)]"
)
sleep(3)
print
(
"\(name) 执行完毕"
)
}
deinit {
//记得移除通知监听
NSNotificationCenter
.defaultCenter().removeObserver(
self
)
}
}
0 0
- Swift - 使用NSNotificationCenter发送通知,接收通知
- NSNotificationCenter通知接收多次
- NSNotificationCenter通知接收多次
- iOS Swift NSNotificationCenter 通知
- Swift里通知的使用NSNotificationCenter
- swift中通知NSNotificationCenter的使用
- 通过NSNotificationCenter发送通知
- NSNotificationCenter发送通知
- 使用NSNotificationCenter 事件通知
- 使用NSNotificationCenter 事件通知
- 使用NSNotificationCenter 事件通知
- 使用NSNotificationCenter 事件通知
- NSNotificationCenter 通知的使用
- NSNotificationCenter 通知的使用
- 发送,接收通知
- [NSNotificationCenter defaultCenter] 通知接收不全面原因
- NSNotificationCenter 通知
- 通知NSNotificationCenter
- Hibernate关于的Timestamp的问题
- 完成OSG3.4编译环境
- App权限问题
- Android使用Font Awesome简单介绍
- 图片模糊化处理
- Swift - 使用NSNotificationCenter发送通知,接收通知
- 判断任意两台计算机的IP地址是否属于同一子网络
- redis的解决方法 (Redis server went away)
- javascript中的super
- Ant多渠道打包
- 关于cell的那点事
- 关于adb push 带空格的文件到机台中
- ICMP
- iOS项目重构周记(二)