NSNotification 译注


NSNotification Class Reference

Inherits from(父类:)    NSObject

Conforms to(遵循协议):    NSCoding  NSCopying  NSObject (NSObject)

Framework    (使用框架):/System/Library/Frameworks/Foundation.framework

Availability    (适用于):Available in iOS 2.0 and later. iOS2.0及以后的版本


Overview(概述)

NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object. An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any. NSNotification objects are immutable objects.

       NSNotification对象将信息封装起来,以便于它通过NSNotificationCenter(通知中心)向其他对象发送这些信息。一个通知对象(被称为通知)包含一个name(通知名),一个object(通知对象)和一个可选择性的dictionary(字典)。通知名name,是识别该通知的标签;object(通知对象),可以是任意的对象,notifacation发出的通知由它作为观察者来接收(特别说明,它是被通知的对象)。如果有字典,字典保存其他相关对象。同志对象一旦创建,将不能被改变。


You can create a notification object with the class methods notificationWithName:object: or notificationWithName:object:userInfo:. However, you don’t usually create your own notifications directly. The NSNotificationCenter methods postNotificationName:object: and postNotificationName:object:userInfo: allow you to conveniently post a notification without creating it first.

      你可以通过类方法 notificationWithName: object: 或者 notificationWithName:  object:  userInfo:来创建一个通知。然而,通常你不会直接创建自己的通知。NSNotificationCenter(通知中心)提供了postNotificationName:  object:和postNotificationName: object:  userInfo:这两个方法,可以让你在不创建自己的通知的情况下很方便的发送一个通知。


NSCopying Protocol(NSCopying协议)

The NSNotification class adopts the NSCopying protocol, making it possible to treat notifications as context-independent values that can be copied and reused. You can store a notification for later use or use the distributed objects system to send a notification to another process. The NSCopying protocol essentially allows clients to deal with notifications as first class values that can be copied by collections. You can put notifications in an array and send the copy message to that array, which recursively copies every item.

      NSNotification类实现了NSCopying协议,从而可以把通知作为能被拷贝和重用的独立上下文值。你可以保存通知以便后来使用,也可以使用系统分布式的对象来给另一个process发送通知。NSCopying协议本质上允许客户端搜集并把通知作为可供拷贝的第一类值来处理。你可以把通知加到数组中,然后向数组发送递归拷贝的拷贝消息。


Creating Subclasses(创建子类)

You can subclass NSNotification to contain information in addition to the notification name, object, and dictionary. This extra data must be agreed upon between notifiers and observers

你可以子类化NSNotification来包含信息,此外还有通知名、通知对象和字典。这个额外数据必须在通知者和观察者之间做好协商。


NSNotification is a class cluster with no instance variables. As such, you must subclass NSNotification and override the primitive methods name, object, and userInfo. You can choose any designated initializer you like, but be sure that your initializer does not call NSNotification’s implementation of init (via [super init]). NSNotification is not meant to be instantiated directly, and its init method raises an exception.

NSNotification是一个没有可变实例的类集。因此,你必须子类化NSNotification,推翻原来的方法名、对象和用户信息。因此你可以根据情况来选择任意特定的便利构造器,但要确保你的便利够构造器没有调用NSNotification的初始化(包括父类初始化)实现方法。NSNotification并不意味着直接初始化,而是它的方法添加了一个特例。



Adopted Protocols(实现的协议)


NSCoding
             – encodeWithCoder:
             – initWithCoder:
NSCopying
             – copyWithZone:


Tasks


Creating Notifications(类方法创建通知)
+ notificationWithName: object:(包含通知名和通知对象)

+ notificationWithName:object:userInfo:(包含通知名、通知对象和用户信息)


Getting Notification Information(get方法,可以用点方法)

– name
– object

– userInfo


Class Methods(类方法)

notificationWithName:object:

Returns a new notification object with a specified name and object

返回一个带有特有名字和对象的新通知。.


+ (id)notificationWithName:(NSString *)aName object:(id)anObject(类方法的全名)

Parameters(参数)
aName 通知名
The name for the new notification. May not be nil.新通知名称,最好不为空
anObject通知对象
The object for the new notification.新通知的对象
Availability
Available in iOS 2.0 and later适用于iOS2.0及以后.
See Also通用
– postNotificationName:object: (NSNotificationCenter)通知中心的方法
Declared In

NSNotification.h由NSNotification.h头文件声明


notificationWithName:object:userInfo(同上,只是多返回一个userinfo字典,其他各项一致):

Returns a notification object with a specified name, object, and user information.

返回一个带有特有名字、对象、用户信息的新通知


+ (id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)userInfo
Parameters
aName
The name for the new notification. May not be nil.
anObject
The object for the new notification.
userInfo
The user information dictionary for the new notification. May be nil.
Availability
Available in iOS 2.0 and later.
See Also
+ notificationWithName:object:
– postNotificationName:object:userInfo: (NSNotificationCenter)
Declared In

NSNotification.h



Instance Methods(实例方法)

name
Returns the name of the notification.

- (NSString *)name
Return Value

The name of the notification. Typically you use this method to find out what kind of notification you are dealing with when you receive a notification.

返回通知的名字。通常,当你接收到一个通知时,调用该方法来确认你正在处理哪个通知。


Special Considerations(特别注意)

Notification names can be any string. To avoid name collisions, you might want to use a prefix that’s specific to your application.

通知名可以时任意的,为了避免冲突,你最好使用跟你的APP对应的前缀。



- (id)object
Return Value

The object associated with the notification. This is often the object that posted this notification. It may be nil.

通知的关联对象,通常是发出通知的对象,可以是空。


Typically you use this method to find out what object a notification applies to when you receive a notification.

通常当你接收到一个通知时,你可以用这个方法找出一个通知适用于哪个对象


Discussion(讨论)

For example, suppose you’ve registered an object to receive the message handlePortDeath: when the “PortInvalid” notification is posted to the notification center and that handlePortDeath: needs to access the object monitoring the port that is now invalid. handlePortDeath: can retrieve that object as shown here:

例如,假使你已经注册了一个对象来接收handlePortDeath这个消息。当通知“PortInvalid”发送到通知中心,handlePortDeath需要使用这个对象监视现在这个不可用的端口。handlePortDeath可以按如下方式接收到这个对象。


- (void)handlePortDeath:(NSNotification *)notification
{
    ...
    [self reclaimResourcesForPort:[notification object]];
    ...
}



userInfo

- (NSDictionary *)userInfo

Return Value

Returns the user information dictionary associated with the receiver. May be nil.

返回跟接收者有关的用户信息字典,可以留空


The user information dictionary stores any additional objects that objects receiving the notification might use.

用户信息字典储存了


Discussion

For example, in the Application Kit, NSControl objects post the NSControlTextDidChangeNotification whenever the field editor (an NSText object) changes text inside the NSControl. This notification provides the NSControl object as the notification's associated object. In order to provide access to the field editor, the NSControl object posting the notification adds the field editor to the notification's user information dictionary. Objects receiving the notification can access the field editor and the NSControl object posting the notification as follows:

例如,在应用工具里,当NSControl里的field editorNSControl对象改变时,NSControl对象发送NSControlTextDidChangeNotification。这个通知把于这个通知相关链的对象提供给NSControl对象,NSControl对象把要添加的field editor加到用户信息字典里。接收到通知的对象可以使用field editor。


- (void)controlTextDidBeginEditing:(NSNotification *)notification
{
    NSText *fieldEditor = [[notification userInfo]
        objectForKey:@"NSFieldEditor"];               // the field editor
    NSControl *postingObject = [notification object]; // the object that posted the notification
    ...
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值