Работает ли NSNotification везде? - PullRequest
       2

Работает ли NSNotification везде?

0 голосов
/ 15 августа 2011

Я отправляю уведомление, используя:

[[NSNotificationCenter defaultCenter] postNotificationName:@"historyLoaded" object:jsonReturn];

и получение уведомления с помощью:

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

Тогда метод в селекторе:

- (void) manageHistory: (NSNotification *) historyData{
     NSLog(@"this bit of code was run");
}

По какой-то причине уведомление не проходит. Можно ли отправлять и получать уведомления из любой точки приложения?

Ответы [ 2 ]

1 голос
/ 15 августа 2011

Параметр object в postNotification должен быть заполнен объектом, который «отправляет» уведомление, или ноль, если отправитель не обязательно указан.
Если вы хотите передать некоторую информацию, вам следуетиспользуйте postNotificationName:object:userInfo и поместите информацию в словарь userInfo.

0 голосов
/ 15 августа 2011
[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(manageHistory) name:@"historyLoaded" object:nil];

[[NSNotificationCenter defaultCenter] postNotificationName:@"historyLoaded" 
       object:nil userInfo:jsonReturn];

- (void) manageHistory: (NSNotification *) historyData{
         NSDictionary* _dict = historyData.userInfo;
         NSLog(@"Your information embedded to dictiuonary obj %@",_dict);
}

ПРИМЕЧАНИЕ. Убедитесь, что ваш historyData должен быть объектом словаря в postNotificationName

...