Résultats de notification iphone dans « sélecteur non reconnu envoyé à l'instance ... »
-
12-10-2019 - |
Question
Pour faire court, je me inscris l'auditeur de NSNotification
suivant ClassA
(en viewDidLoad
):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playSong) name:@"playNotification" object:nil];
Je le sélecteur déclaré dans ClassA.h
:
- (void)playSong:(NSNotification *) notification;
Et la mise en œuvre comme suit va:
- (void)playSong:(NSNotification *) notification {
NSString *theTitle = [notification object];
NSLog(@"Play stuff", theTitle);
}
ClassB
(dans le procédé de tableView:didSelectRowAtIndexPath:
) je:
NSInteger row = [indexPath row];
NSString *stuff = [playlistArray objectAtIndex:row];
[[NSNotificationCenter defaultCenter] postNotificationName:@"playNotification" object:stuff];
Tout fin avec un message d'erreur indiquant:
"sélecteur non reconnu envoyé à l'instance"
avant que la méthode playSong
est invoquée.
Quelqu'un peut-il s'il vous plaît aidez-moi ici? Qu'est-ce que j'oubliais lors de la publication d'une notification d'un contrôleur à l'autre?
La solution
Votre @selector
a besoin d'un caractère :
si elle est de prendre un argument:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playSong:) name:@"playNotification" object:nil];
Les instances de ClassA
font pas répondre au sélecteur de playSong
, mais faire répondre au sélecteur de playSong:
.