SORU
30 Kasım 2010, Salı


nasıl NSNotificationcenter nesne özelliğini kullanın

Biri NSNotifcationCenter nesne özelliği nasıl bana göster lütfen. Bunu kullanabilmek için seçici benim yöntemi bir tamsayı geçmek istiyorum.

Bu UI benim görüşüme göre bildirim dinleyici hazırladım. Geçirilmesi için bir tamsayı değeri istediğim gibi görmekten nil değiştirmek için ne olduğundan emin değilim.

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


- (void)receiveEvent:(NSNotification *)notification {
    // handle event
    NSLog(@"got event %@", notification);
}

Ben bunun gibi başka bir sınıftan satış bildirimi. Bu fonksiyon bir değişken dizin adlı geçirilir. Bir şekilde bildirim ile ateş istediğim bu değer.

-(void) disptachFunction:(int) index
{
    int pass= (int)index;

    [[NSNotificationCenter defaultCenter] postNotificationName:@"myevent" object:pass];
    //[[NSNotificationCenter defaultCenter] postNotificationName:<#(NSString *)aName#>   object:<#(id)anObject#>
}

CEVAP
30 Kasım 2010, Salı


object Bu özellik için uygun değil. Bunun yerine userinfo parametre kullanmak istediğiniz:

  (id)notificationWithName:(NSString *)aName 
                    object:(id)anObject 
                  userInfo:(NSDictionary *)userInfo

userInfo gördüğünüz gibi, bir NSDictionary bildirimi ile birlikte gönderme bilgi için özellikle.

dispatchFunction yöntem bu gibi bir şey olurdu:

- (void) disptachFunction:(int) index {
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:index] forKey:@"pass"];
   [[NSNotificationCenter defaultCenter] postNotificationName:@"myevent" object:nil userInfo:userInfo];
}

receiveEvent yöntem bu gibi bir şey olacaktır:

- (void)receiveEvent:(NSNotification *)notification {
    int pass = [[[notification userInfo] valueForKey:@"pass"] intValue];
}

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Chuck Testa

    Chuck Testa

    14 AĞUSTOS 2011
  • DrePwn

    DrePwn

    22 Temmuz 2011
  • Sean Murphy

    Sean Murphy

    4 ŞUBAT 2009