如何侦听发送到iOS NSNotificationCenter的defaultCenter的所有通知? [英] How can I listen for all notifications sent to the iOS NSNotificationCenter's defaultCenter?

查看:976
本文介绍了如何侦听发送到iOS NSNotificationCenter的defaultCenter的所有通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想收听调度到defaultCenter的所有通知。公共和私人。有谁知道我怎么做?

I want to listen to all notifications dispatched to the defaultCenter. Both public and private. Does anyone know how I can do this?

推荐答案

使用NSNotificationCenter的 addObserverForName:object :queue:usingBlock: addObserver:selector:name:object: method and pass nil为名称和对象。

Use NSNotificationCenter's addObserverForName:object:queue:usingBlock: OR addObserver:selector:name:object: method and pass nil for the name and object.

以下代码应该可以完成这项工作:

The following code should do the job:

- (void)dumpNotifications {
    NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter];
    [notifyCenter addObserverForName:nil 
                              object:nil 
                               queue:nil 
                          usingBlock:^(NSNotification *notification){
                             // Explore notification
                             NSLog(@"Notification found with:"
                                    "\r\n     name:     %@"
                                    "\r\n     object:   %@"
                                    "\r\n     userInfo: %@", 
                                    [notification name], 
                                    [notification object], 
                                    [notification userInfo]);
                          }];
}



文档



以下是 addObserverForName:object:queue:usingBlock: 。特别是,请参阅名称 obj 参数。


addObserverForName :object:queue:usingBlock:

使用通知
队列和要添加到队列的块向接收者的调度表添加一个条目和可选标准:
通知名称和发件人。

Adds an entry to the receiver’s dispatch table with a notification queue and a block to add to the queue, and optional criteria: notification name and sender.

- (id)addObserverForName:(NSString *)name object:(id )obj
队列:(NSOperationQueue *)队列usingBlock :( void(^)(NSNotification
*))block

参数

名称

名称注册观察员的通知;
是,只有具有此名称的通知用于将块添加到
操作队列。 如果您通过nil,,通知中心不会使用
a通知的名称来决定是否将该块添加到
操作队列。

The name of the notification for which to register the observer; that is, only notifications with this name are used to add the block to the operation queue. If you pass nil, the notification center doesn’t use a notification’s name to decide whether to add the block to the operation queue.

obj

要将其块添加到
操作队列的通知的对象。 如果您通过nil,,通知中心不会使用
a通知的发件人来决定是否将该块添加到
操作队列。

The object whose notifications you want to add the block to the operation queue. If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to add the block to the operation queue.

队列

应添加块的操作队列。如果你传递nil,
该块在发布帖子上同步运行。

The operation queue to which block should be added. If you pass nil, the block is run synchronously on the posting thread.

block

收到通知时要执行的块。块
由通知中心复制并保留(副本),直到
观察员注册被删除。该块有一个参数:

The block to be executed when the notification is received. The block is copied by the notification center and (the copy) held until the observer registration is removed. The block takes one argument:

通知

通知。

这篇关于如何侦听发送到iOS NSNotificationCenter的defaultCenter的所有通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆