2012-04-23 5 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メソッドまたはあなたがdeallocメソッドではオブザーバーとして、あなたのオブジェクトを削除する必要のviewDidLoadメソッド

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

ノートにあるかもしれません。

[[NSNotificationCenter defaultCenter] removeObserver:self] 
+0

ご協力いただきありがとうございます。しかし、どのようにして** addObserver **をしてから** postNotificationName **を後で行うのですか? [ここ](http://stackoverflow.com/questions/10283014/can-not-catch-a-notification-in-iphone)で、私はあなたと同じことをしますが、**セレクタ**は存在していませんすべての後に呼ばれます。 – tranvutuan

+0

はい、通知を送信する前にオブザーバーを追加する必要があることを明確にするため。通常、これはinitまたはviewdidloadメソッドにあります。 –

+0

私はさらにいくつかの例が必要です。postnotificationsに関するいくつかの例のリンクをお願いします。 – vijay

関連する問題