Domanda

Per farla breve, sto registrando il seguente ascoltatore NSNotification a ClassA (in viewDidLoad):

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

Ho il selettore dichiarata in ClassA.h:

- (void)playSong:(NSNotification *) notification;

E implementazione è la seguente:

- (void)playSong:(NSNotification *) notification {
    NSString *theTitle = [notification object]; 
    NSLog(@"Play stuff", theTitle);
}

In ClassB (nel metodo tableView:didSelectRowAtIndexPath:) ho:

NSInteger row = [indexPath row];
NSString *stuff = [playlistArray objectAtIndex:row];
[[NSNotificationCenter defaultCenter] postNotificationName:@"playNotification" object:stuff];

Tutto finisce con un messaggio di errore che dice:

"selettore riconosciuto inviato ad esempio"

prima che il metodo playSong viene richiamato.

Qualcuno può please help me qui? Cosa sto dimenticando quando la pubblicazione di una notifica da un controller a un altro?

È stato utile?

Soluzione

Il tuo @selector ha bisogno di un personaggio : se si tratta di prendere un argomento:

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

Le istanze di ClassA do non rispondere al selettore playSong, ma do rispondere al selettore playSong:.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top