Передача userInfo (NSDictionary) другому объекту с помощью NSNotication - PullRequest
0 голосов
/ 17 октября 2011

Я пытаюсь использовать NSNotification. Я создаю NSDictionary на основе того, какая из моих кнопок была нажата в представлении. Когда я NSLog тег, тег является правильным. Поэтому я использую оператор switch, как этот, чтобы создать свой словарь:

NSDictionary *dict;
switch (tag) {
    case 0:
        dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:tag] forKey:@"ButtonType"];
        break;
    case 1:
        dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:tag], @"ButtonType", nil];
        break;
    case 2:
        dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:tag], @"ButtonType", nil];
        break;
    case 3:
        dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:tag], @"ButtonType", nil];
        break;
    case 4:
        dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:tag], @"ButtonType", nil];
        break;
    default:
        NSLog(@"No dictionary set for ButtonType");
        break;
}
[[NSNotificationCenter defaultCenter] postNotificationName:(ShowData:) object:self userInfo:dict];

А затем в ShowData:

- (void)ShowData:(NSNotification *)notification {
    NSLog(@"%s", __FUNCTION__);
    NSDictionary *userDict = [notification userInfo];
    NSInteger theTag = (NSInteger)[userDict objectForKey:@"ButtonType"];
    NSLog(@"tag: %i", theTag); // this value is incorrect

В моем методе ShowData я ожидал бы те же значения тега, 0-4, но вместо этого я получаю что-то вроде: 187742848. Когда я перехожу через отладчик, я вижу, что мой userInfo NSDictionary устанавливается в значение, которое передается , Я не создаю свой NSDictionary правильный, чтобы передать ShowData:? Спасибо.

Ответы [ 2 ]

2 голосов
/ 17 октября 2011

Вместо приведения объекта, возвращенного из словаря, вызовите integerValue.

[[userDict objectForKey:@"ButtonType"] integerValue];
0 голосов
/ 17 октября 2011

Полагаю, я не смог разыграть NSNumber в NSInteger. Мне нужно было использовать integerValue из NSNumber, и я получил правильное значение тега.

...