Comment poster et recevoir un NSNotifications (Objective C) | Notifications (Swift)?
-
28-09-2019 - |
Question
Y at-il un modèle facile à Grock comment envoyer un NSNotification (Objective C) | Notification (à Swift) et comment recevoir un? Extrait de code? Les documents écrivent comme 150 pages sur le sujet. Aimeriez-vous voir un exemple rapide.
La solution
Envoyer une notification:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyCacheUpdatedNotification" object:self];
Recevez-le:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cacheUpdated:) name:@"MyCacheUpdatedNotification" object:nil];
Loi sur elle:
- (void)cacheUpdated:(NSNotification *)notification {
[self load];
}
et jetez-:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Autres conseils
Swift Version pour le même:
Chaque fois que vous devez afficher la notification:
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "UpdateAccepted"), object: self)
Le contrôleur où vous souhaitez recevoir la notification:
override func viewDidLoad(_ animated: Bool) {
super.viewDidLoad(true) {
NotificationCenter.default.addObserver(self, selector: #selector(updateAccepted(notification:)), name: NSNotification.Name(rawValue: "UpdateAccepted"), object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "UpdateAccepted"), object: nil)
}
@objc func updateAccepted(notification: Notification) {
handleRefresh()
}
-
Inscrivez-vous un profil de notification push et le mettre dans votre Heres application un lien pour le faire C # notification push src
Cest à peu près tout ... je lui ai donné une solution rapide ua utilisant une technologie .Net, si vous utilisez somethign que vous pouvez parcourir pour voir s'il existe des solutions disponibles dans la plate-forme que vous utilisez im YOULL sûr trouver quelque chose, si vous pouvez pas faire votre propre alwyas :)