Ich versuche, Nachrichten NSNotificationCenter
in Objective-C zu senden und zu empfangen . Ich konnte jedoch keine Beispiele dafür finden. Wie senden und empfangen Sie Nachrichten NSNotificationCenter
?
Ich versuche, Nachrichten NSNotificationCenter
in Objective-C zu senden und zu empfangen . Ich konnte jedoch keine Beispiele dafür finden. Wie senden und empfangen Sie Nachrichten NSNotificationCenter
?
Antworten:
@implementation TestClass
- (void) dealloc
{
// If you don't remove yourself as an observer, the Notification Center
// will continue to try and send notification objects to the deallocated
// object.
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (id) init
{
self = [super init];
if (!self) return nil;
// Add this instance of TestClass as an observer of the TestNotification.
// We tell the notification center to inform us of "TestNotification"
// notifications using the receiveTestNotification: selector. By
// specifying object:nil, we tell the notification center that we are not
// interested in who posted the notification. If you provided an actual
// object rather than nil, the notification center will only notify you
// when the notification was posted by that particular object.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
return self;
}
- (void) receiveTestNotification:(NSNotification *) notification
{
// [notification name] should always be @"TestNotification"
// unless you use this method for observation of other notifications
// as well.
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
}
@end
... woanders in einer anderen Klasse ...
- (void) someMethod
{
// All instances of TestClass will be notified
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:self];
}
[super dealloc]
Aufruf in der Dealloc-Methode unter ARC nicht zulässig ist; der Rest ist alles gut.
Um das Beispiel von dreamlax zu erweitern ... Wenn Sie Daten zusammen mit der Benachrichtigung senden möchten
Im Buchungscode:
NSDictionary *userInfo =
[NSDictionary dictionaryWithObject:myObject forKey:@"someKey"];
[[NSNotificationCenter defaultCenter] postNotificationName:
@"TestNotification" object:nil userInfo:userInfo];
Bei der Beobachtung des Codes:
- (void) receiveTestNotification:(NSNotification *) notification {
NSDictionary *userInfo = notification.userInfo;
MyObject *myObject = [userInfo objectForKey:@"someKey"];
}
self
Methode "receiveTestNotification" auf den Beobachter zugreifen?
Dieser hat mir geholfen:
// Add an observer that will respond to loginComplete
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(showMainMenu:)
name:@"loginComplete" object:nil];
// Post a notification to loginComplete
[[NSNotificationCenter defaultCenter] postNotificationName:@"loginComplete" object:nil];
// the function specified in the same class where we defined the addObserver
- (void)showMainMenu:(NSNotification *)note {
NSLog(@"Received Notification - Someone seems to have logged in");
}
Quelle: http://www.smipple.net/snippet/Sounden/Simple%20NSNotificationCenter%20example
Es besteht auch die Möglichkeit, Blöcke zu verwenden:
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter]
addObserverForName:@"notificationName"
object:nil
queue:mainQueue
usingBlock:^(NSNotification *notification)
{
NSLog(@"Notification received!");
NSDictionary *userInfo = notification.userInfo;
// ...
}];
Wenn Sie NSNotificationCenter zum Aktualisieren Ihrer Ansicht verwenden, vergessen Sie nicht, sie vom Hauptthread aus zu senden, indem Sie Folgendes aufrufen dispatch_async
:
dispatch_async(dispatch_get_main_queue(),^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"my_notification" object:nil];
});
SWIFT 5.1 der ausgewählten Antwort für Neulinge
class TestClass {
deinit {
// If you don't remove yourself as an observer, the Notification Center
// will continue to try and send notification objects to the deallocated
// object.
NotificationCenter.default.removeObserver(self)
}
init() {
super.init()
// Add this instance of TestClass as an observer of the TestNotification.
// We tell the notification center to inform us of "TestNotification"
// notifications using the receiveTestNotification: selector. By
// specifying object:nil, we tell the notification center that we are not
// interested in who posted the notification. If you provided an actual
// object rather than nil, the notification center will only notify you
// when the notification was posted by that particular object.
NotificationCenter.default.addObserver(self, selector: #selector(receiveTest(_:)), name: NSNotification.Name("TestNotification"), object: nil)
}
@objc func receiveTest(_ notification: Notification?) {
// [notification name] should always be @"TestNotification"
// unless you use this method for observation of other notifications
// as well.
if notification?.name.isEqual(toString: "TestNotification") != nil {
print("Successfully received the test notification!")
}
}
}
... woanders in einer anderen Klasse ...
func someMethod(){
// All instances of TestClass will be notified
NotificationCenter.default.post(name: "TestNotification", object: self)
}