我需要在两个不同的控制台应用程序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 …Run Code Online (Sandbox Code Playgroud) 我在info.plist中添加了值为1的NSUIElement,但是停靠图标仍然出现。这是我的代码
<key>NSUIElement</key>
<string>1</string>
Run Code Online (Sandbox Code Playgroud)