SORU
25 EKİM 2011, Salı


Nasıl NSNotificationCenter ile nesne geçmek

Başka bir sınıfta bir bildirim alıcı için app temsilcime bir nesne geçmek için çalışıyorum.

Tam sayı messageTotal geçmek istiyorum. Şimdi var değil mi:

Alıcı:

- (void) receiveTestNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"TestNotification"])
        NSLog (@"Successfully received the test notification!");
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissSheet) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"eRXReceived" object:nil];

Sınıfta bu bildirim gidiyor

[UIApplication sharedApplication].applicationIconBadgeNumber = messageTotal;
[[NSNotificationCenter defaultCenter] postNotificationName:@"eRXReceived" object:self];

Ama nesne messageTotal başka bir sınıfa geçmek istiyorum.

CEVAP
25 EKİM 2011, Salı


"Kullanıcı bilgisi" ve messageTotal tamsayı içeren NSDictionary bir nesne değişken geçirin: . kullanman gerekecek

NSDictionary* userInfo = @{@"total": @(messageTotal)};

NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"eRXReceived" object:self userInfo:userInfo];

Alan tarafta aşağıdaki gibi kullanıcı bilgisi sözlüğü erişebilirsiniz

-(void) receiveTestNotification:(NSNotification*)notification
{
    if ([notification.name isEqualToString:@"TestNotification"])
    {
        NSDictionary* userInfo = notification.userInfo;
        NSNumber* total = (NSNumber*)userInfo[@"total"];
        NSLog (@"Successfully received test notification! %i", total.intValue);
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Ampisound

    Ampisound

    12 Kasım 2006
  • Caroline Martin

    Caroline Mar

    19 EYLÜL 2008
  • picster

    picster

    20 NİSAN 2006