Donner une notification à une autre classe avec NSNotificationCenter
-
25-10-2019 - |
Question
Donc, mon objectif est de fournir une notification à une autre classe avec l'aide NSNotificationCenter
, je veux aussi passer object
la notification à l'autre class
, comment dois-je faire?
La solution
Vous devez d'abord enregistrer un nom de notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startLocating:) name:@"ForceUpdateLocation" object:nil]; // don't forget the ":"
Et puis après une notification avec un dictionnaire de paramètres
[[NSNotificationCenter defaultCenter] postNotificationName:@"ForceUpdateLocation" object:self userInfo:[NSDictionary dictionaryWithObject:@"1,2,3,4,5" forKey:@"categories_ids"]];
et la méthode sera
- (void)startLocating:(NSNotification *)notification {
NSDictionary *dict = [notification userInfo];
}
Autres conseils
Il suffit d'appeler une méthode pour les notifications d'affichage comme ici , par exemple:
pour afficher une notification:
-(void)postNotificationName:(NSString *)notificationName
object:(id)notificationSender
userInfo:(NSDictionary *)userInfo;
où userInfo
est un dictionnaire contenant des objets utiles.
De l'autre côté pour vous inscrire pour les notifications:
-(void)addObserver:(id)notificationObserver
selector:(SEL)notificationSelector
name:(NSString *)notificationName
object:(id)notificationSender;
Vous pouvez également consulter d'Apple programmation de notification Sujets .
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow