2012-04-23 41 views

回答

18

您可以在API調用的userDictionary元素中傳遞數據

NSDictionary *aDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: 
          anObject, @"objectName", 
          anotherObject, @"objectId", 
          nil] autorelease]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"AnythingAtAll" object:nil userInfo:aDictionary]; 

您可以從您觀察到的入站通知中檢索字典。在發佈通知之前添加觀察員。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(anyAction:) name:@"AnythingAtAll" object:nil]; 

,這可能是在你的init方法或viewDidLoad方法

-(void)anyAction:(NSNotification *)anote 
{ 
NSDictionary *dict = [anote userInfo]; 
AnyClass *objectIWantToTransfer = [dict objectForKey:@"objectName"]; 
} 

請注意,您應該刪除您的對象在dealloc方法的觀察員。

[[NSNotificationCenter defaultCenter] removeObserver:self] 
+0

感謝您的幫助。然而,你是如何讓你**首先添加觀察者**然後再**後postNotificationName **。在[這裏](http://stackoverflow.com/questions/10283014/can-not-catch-a-notification-in-iphone),我做的和你一樣的事情,但**選擇器**不是畢竟是所謂的。 – tranvutuan 2012-04-24 13:15:38

+0

是的,以澄清你需要在發佈通知之前添加觀察者。通常這將在init或viewdidload方法中。 – 2012-04-24 20:15:05

+0

我需要一些更多的例子..請舉一些關於postnotifications的例子鏈接。 – vijay 2015-02-06 05:33:08