Cocoa NSNotificationCenter 应用程序之间通信失败
我需要在两个不同的控制台应用程序(观察者和客户端)之间进行通信。
在观察者应用程序中,我添加了以下代码:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];
在客户端应用程序中,我添加了以下代码:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];
-(void)trackNotification:(NSNotification*)notif
{
NSLog(@"trackNotification: %@", notif);
NSLog(@"trackNotification name: %@", [notif name]);
NSLog(@"trackNotification object: %@", [notif object]);
NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}
但没有任何反应。我阅读了所有文档,但我对 Mac OS X 是全新的。 有什么想法吗?
我阅读了有关分布式通知的信息,我更改了代码,现在看起来像这样: 在服务器端:
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];
-(void)trackNotification:(NSNotification*)notif
{
NSLog(@"trackNotification: %@", notif);
NSLog(@"trackNotification name: %@", [notif name]);
NSLog(@"trackNotification object: %@", [notif object]);
NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}
在客户端:
NSMutableDictionary *info;
info = [NSMutableDictionary dictionary];
[info setObject:@"foo" forKey:@"bar"];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
object:nil
userInfo:info
deliverImmediately:YES];
我运行两个应用程序,但没有任何反应。我做错了什么?
I need to communicate between two different console apps, Observer and Client.
In the Observer app I added this code:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];
In the Client app I added this code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];
-(void)trackNotification:(NSNotification*)notif
{
NSLog(@"trackNotification: %@", notif);
NSLog(@"trackNotification name: %@", [notif name]);
NSLog(@"trackNotification object: %@", [notif object]);
NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}
but nothing happens. I read all the documentation, but I am brand new in Mac OS X.
Any ideas?
I read about the Distributed Notifications, I changed my code and now looks like this:
In the server side:
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];
-(void)trackNotification:(NSNotification*)notif
{
NSLog(@"trackNotification: %@", notif);
NSLog(@"trackNotification name: %@", [notif name]);
NSLog(@"trackNotification object: %@", [notif object]);
NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}
In the client side:
NSMutableDictionary *info;
info = [NSMutableDictionary dictionary];
[info setObject:@"foo" forKey:@"bar"];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
object:nil
userInfo:info
deliverImmediately:YES];
I run both applications, but nothing happens. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSNotificationCenter
仅适用于一个应用程序内的通知。您想要 NSDistributedNotificationCenter。您可以在通知编程主题中找到更多详细信息。NSNotificationCenter
is only for notifications within one app. You want NSDistributedNotificationCenter. You can find more details in the Notification Programming Topics.我解决了我的问题。这是源代码:
在客户端:
在服务器端
receiveNotification 定义
在 dealloc 方法中
I solved my problem. This is the source code:
In the client side:
In the server side
receiveNotification definition
In dealloc method