Ana*_*na 2 cocoa nsnotifications nsnotificationcenter
我需要在两个不同的控制台应用程序Observer和Client之间进行通信.
在Observer应用程序中,我添加了以下代码:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];
Run Code Online (Sandbox Code Playgroud)
在客户端应用中,我添加了以下代码:
[[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]);
}
Run Code Online (Sandbox Code Playgroud)
但没有任何反应.我阅读了所有文档,但我在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]);
}
Run Code Online (Sandbox Code Playgroud)
在客户端:
NSMutableDictionary *info;
info = [NSMutableDictionary dictionary];
[info setObject:@"foo" forKey:@"bar"];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
object:nil
userInfo:info
deliverImmediately:YES];
Run Code Online (Sandbox Code Playgroud)
我运行两个应用程序,但没有任何反应.我究竟做错了什么?
| 归档时间: |
|
| 查看次数: |
2088 次 |
| 最近记录: |