NSNotificationCenter译注


NSNotificationCenter Class Reference

Inherits from    (继承自):NSObject
Conforms to        (遵循):NSObject (NSObject)
Framework          (框架   :/System/Library/Frameworks/Foundation.framework
Availability           (适用   :Available in iOS 2.0 and later.
Declared in            (声明)  : NSNotification.h

Overview(概述)

An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table.

通知中心对象(或通知中心)提供了在程序内广播消息的机制。通知中心在本质上是一个信息调度表。


Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.

对象通过addObserver:selector:name:object:或者addObserverForName:object:queue:usingBlock:方法向通知中心登记,来接收通知(通知对象)。每次调用方法指定一个通知集合,因而,对象可以通过多次调用这些方法注册为不同通知的观察者。


Each running Cocoa program has a default notification center. You typically don’t create your own. An NSNotificationCenter object can deliver notifications only within a single program. If you want to post a notification to other processes or receive notifications from other processes, use an instance of NSDistributedNotificationCenter.

每个运行中的Cocoa(cocoa框架)程序都有一个默认的notification center(通知中心)。通常你不需要自己创建通知中心。通知中心对象只可以在单进程中发送通知,如果你想给其他进程发送通知或接收其他进程发出的通知,需要用到NSDistributedNotificationCenter(散部式通知中心)的实例。


Tasks

Getting the Notification Center(获得通知中心)
+ defaultCenter
Managing Notification Observers(编辑通知)
– addObserverForName:object:queue:usingBlock:
– addObserver:selector:name:object:
– removeObserver:
– removeObserver:name:object:
Posting Notifications(发送通知)
– postNotification:
– postNotificationName:object:

– postNotificationName:object:userInfo:


Class Methods(类方法)


defaultCenter(默认通知)

Returns the process’s default notification center.

返回当前进程的通知中心单例


+ (id)defaultCenter
Return Value

The current process’s default notification center, which is used for system notifications.

当前进程的默认通知中心,用来发送系统通知。



Instance Methods(实例方法)

addObserver:selector:name:object:

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

添加一个到接收者的调度表的入口,该入口包含:observer(观察者)、通知方法、可选条件:通知名和发送者。


- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
Parameters(参数)
notificationObserver:通知的观察者

Object registering as an observer. This value must not be nil.

对象注册为观察者,该参数值不能为空。

notificationSelector:通知方法

Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method specified by notificationSelector must have one and only one argument (an instance of NSNotification).

选择器,用于指定通知接收者发给通知观察者发布通知的消息(该句我不会翻译)。这个方法说明了通知选择器有且只能有一个argument(通知对象)。

notificationName:通知名

The name of the notification for which to register the observer; that is, only notifications with this name are delivered to the observer.

通知名称,与注册的观察者对应;也就是说,只有该名字的通知才能传达给给观察者。

If you pass nil, the notification center doesn’t use a notification’s name to decide whether to deliver it to the observer.

如果你把通知名留空,通知中心不会用通知名决定是否给观察者发送通知。

notificationSender:(通知发送者)

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer

观察者接收该对象发出的通知,也就是说,只有发送者发出通知时,观察者才有可能接收到通知。.

If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

当你把观察者留空时,通知中心不会用通知的发送者来决定是否把通知发送给观察者。


Discussion(注意)
Be sure to invoke removeObserver: or removeObserver:name:object: before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated.

确保在通知的观察者或任何在addObserver:selector:name:object:这个方法里指定了的对象释放前,把观察者从当前类里移除掉。


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 queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *))block
Parameters(参数)
name

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

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.

留空时,通知中心不会用通知的发送者来决定是否将代码块添加到操作队列里。

queue

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:代码块取用一个参数(通知对象)。
notification
The notification.要添加的通知
Return Value:返回值
An opaque object to act as the observer.一个作为观察者的不透明对象。

Discussion:讨论

If a given notification triggers more than one observer block, the blocks may all be executed concurrently withrespect to one another (but on their given queue or on the current thread).

如果一个给定的通知触发了不止一个观察者块,这些代码块可能在互不影响的情况下并发执行(只限于他们所属的队列或当前线程)。

The following example shows how you can register to receive locale change notifications.示例

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
self.localeChangeObserver = [center addObserverForName:NSCurrentLocaleDidChangeNotification object:nil
    queue:mainQueue usingBlock:^(NSNotification *note) {
 
        NSLog(@"The user's locale changed to: %@", [[NSLocale currentLocale] localeIdentifier]);

    }];


To unregister observations, you pass the object returned by this method to removeObserver:. You must invoke removeObserver: or removeObserver:name:object: before any object specified by addObserverForName:object:queue:usingBlock: is deallocated.

通过removeObserver:这个方法返回的对象来注销观察者。在addObserverForName:object:queue:usingBlock: is deallocated这个方法中任意声明过的对象释放前,你必须调用removeObserver: 或 removeObserver:name:object:方法来移除观察者。


NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self.localeChangeObserver];
Availability
Available in iOS 4.0 and later.



postNotification:
Posts a given notification to the receiver.推送一个已给出的通知

- (void)postNotification:(NSNotification *)notification
Parameters
notification

The notification to post. This value must not be nil.

要推送的通知,该参数不可为空。

Discussion:讨论

You can create a notification with the NSNotification class method notificationWithName:object: or notificationWithName:object:userInfo:. An exception is raised if notification is nil.你可以用NSNotification的类方法notificationWithName:object: 或者 notificationWithName:object:userInfo:来创建一个通知。如果通知为空,就会出现例外。

See Also:参照

– postNotificationName:object:
– postNotificationName:object:userInfo:


postNotificationName:object:

Creates a notification with a given name and sender and posts it to the receiver.

创建一个带有名字和发送者的通知,并发送。


- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender
Parameters
notificationName
The name of the notification.通知名
notificationSender
The object posting the notification.通知的发送者
Discussion

This method invokes postNotificationName:object:userInfo: with a userInfo argument of nil.

该方法调用了postNotificationName:object:userInfo:这个方法,只是userInfo参数为空。



postNotificationName:object:userInfo:

Creates a notification with a given name, sender, and information and posts it to the receiver.

创建一个带有名称、发送者和信息的通知,并将它发送给接收者。


- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo
Parameters
notificationName
The name of the notification.
notificationSender
The object posting the notification.
userInfo
Information about the the notification. May be nil.通知信息,可以留空。
Discussion

This method is the preferred method for posting notifications.

该方法事推送通知方法中的优先方法。



removeObserver:

Removes all the entries specifying a given observer from the receiver’s dispatch table.

从接受者的调度表中将给定的观察者的所有入口清除掉。简单讲就是清除观察者。


- (void)removeObserver:(id)notificationObserver
Parameters
notificationObserver

The observer to remove. Must not be nil要清除的观察者不能为空。


Discussion:讨论下

Be sure to invoke this method (or removeObserver:name:object:) before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated.

在任何addObserver:selector:name:object: 方法中指定的对象释放前,确保调用该方法或者removeObserver:name:object:这个方法将观察者清除。

The following example illustrates how to unregister someObserver for all notifications for which it had previously registered:

如下示例阐述了如何注销之前注册过的观察者。

[[NSNotificationCenter defaultCenter] removeObserver:someObserver];



removeObserver:name:object:

Removes matching entries from the receiver’s dispatch table.

从接收者的调度表中清除匹配的条目。


- (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender
Parameters
notificationObserver

Observer to remove from the dispatch table. Specify an observer to remove only entries for this observer. Must not be nil, or message will have no effect.

将要从调度标中清除的观察者。指定观察者清除他自己的某个条目。不可以为空,

notificationName

Name of the notification to remove from dispatch table. Specify a notification name to remove only entries that specify this notification name. When nil, the receiver does not use notification names as criteria for removal.

要从调度表上清除的通知的名称。指定一个要清除的通知的名称,清除对应该指定名称的唯一条目。当名称为空时,接收者不会用通知名来作为清除通知的标准。


notificationSender

Sender to remove from the dispatch table. Specify a notification sender to remove only entries that specify this sender. When nil, the receiver does not use notification senders as criteria for removal.从调度表上清除的通知发送者。制定一个通知发送者清除跟他自己对应的唯一入口。当它为空时,接收者不用发送者来做清除的标准。


Discussion

Be sure to invoke this method (or removeObserver:) before the observer object or any object specified in addObserver:selector:name:object: is deallocated.

相关对象释放前,确保调用该方法或者removeObserver:。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值