2011-07-24 45 views

回答

7

您必須先註冊一個通知名

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startLocating:) name:@"ForceUpdateLocation" object:nil]; // don't forget the ":" 

然後張貼通知,參數

[[NSNotificationCenter defaultCenter] postNotificationName:@"ForceUpdateLocation" object:self userInfo:[NSDictionary dictionaryWithObject:@"1,2,3,4,5" forKey:@"categories_ids"]]; 

和字典的方法將是

- (void)startLocating:(NSNotification *)notification { 

    NSDictionary *dict = [notification userInfo]; 
} 
+0

我如何訪問userInfo在我的方法中被調用? –

+0

我會用更多的代碼編輯我的答案。 – pasine

+0

@notme - 如果您發佈並註冊相同的通知名稱,情況會不會更好? – 2011-07-24 22:30:04

0

只需撥打任何方法如所描述的here,例如發佈通知:

來發布通知:

-(void)postNotificationName:(NSString *)notificationName 
        object:(id)notificationSender 
        userInfo:(NSDictionary *)userInfo; 

其中userInfo是含有有用的對象的字典。

在對方註冊通知:

-(void)addObserver:(id)notificationObserver 
      selector:(SEL)notificationSelector 
       name:(NSString *)notificationName 
      object:(id)notificationSender; 

你也可以查看蘋果的Notification Programming Topics

相關問題