2011-06-03 64 views
0

我在确定如何分辨哪个对象发布的通知的麻烦。确定哪个对象发布通知?

予订阅通知在对象A

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) [email protected]"ReceivedData" object:nil] 

我发布通知从对象B

[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedData" object: self userInfo: dict]; 

我收到该通知中对象A

- (void) receivedNotification: (NSNotification*) notification 
{ 
    // Method is hit fine, notification object contains data. 
} 

我怎么能知道它是对象B发送的数据而不是,例如,一个对象C?我需要对发件人的引用。我不想将发件人添加到正在传递的通知对象,因为我在拨打对象时指定发件人B

回答

3

NSNotification的类有一个名为object方法返回与所述通知相关联的对象。这通常是发布此通知的对象。

- (void) receivedNotification: (NSNotification*) notification 
{ 
    ... 
    id myObject = [notification object]; 
    ... 
} 
+0

这正是我想要检索的内容。我如何在通知的接收者中获得该信息?你可以看到我的问题,发布通知的类包括object:self。 – 2011-06-03 16:51:09

+0

在通知参数上调用'object'方法。 – albertamg 2011-06-03 16:53:51

0

如果您只想处理来自B类的通知,则指定随着对象(你已经离开为nil)订阅该通知时。

随着nil,您从发布的具体通知所有对象接收通知。

编辑

你叫[notification object]知道什么对象张贴的通知。

+0

我不想仅限于B类的通知,我想确定通知的发件人。它来自哪里并不重要。我只需要对该对象的引用。我计划从通知中释放对象。 – 2011-06-03 16:46:57

+0

更新我的问题 - 使用术语类不清楚。 – 2011-06-03 16:48:38

+0

我已经更新了我的答案。 – Abizern 2011-06-03 16:49:11