开发者

Cannot get Cocoa notifications to work

开发者 https://www.devze.com 2023-01-16 17:31 出处:网络
Settting up the observer code: NSNotificationCente开发者_高级运维r *defaultCenter = [[NSWorkspace sharedWorkspace] notificationCenter];

Settting up the observer code:

NSNotificationCente开发者_高级运维r *defaultCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
[defaultCenter addObserver:self
selector:@selector(updateLog:)
    name:@"Update Log"
  object:nil];

Sending the notification code:

[[NSNotificationCenter defaultCenter] postNotificationName:@"Update Log" object:self];

With the method defined as:

-(void)updateLog: (NSNotification *) notification {
NSLog(@"Update Log"); }

The text "Update Log" does not appear in the log when the notification is sent. Thanks for any ideas for why this code is not working.


There is a difference between "the notification center for workspace notifications" Apple:

[[NSWorkspace sharedWorkspace] notificationCenter]

and "the process’s default notification center" Apple:

[NSNotificationCenter defaultCenter]

You need to pick one of those to use.

0

精彩评论

暂无评论...
验证码 换一张
取 消