我正在为 TextEdit 开发一个 SIMBL 插件,该插件添加了一个额外的菜单。菜单有许多项目(除其他事项)时,将消息发送到我自己的应用程序。为此,我一直在使用 NSDistributedNotificationCenter,但是我无法从 TextEdit 中的包发送通知。
我检查了我在一个单独的应用程序中使用的代码,它似乎可以工作,所以我不明白为什么我在 TextEdit 中的包无法发送消息。
发布通知代码:
NSDictionary *user = [NSDictionary dictionaryWithObjectsAndKeys:
@"TextEdit", @"applicationName",
@"289", @"applicationVersion",
@"1", @"menuItem", nil];
NSString *observedObject = @"com.drake.DDX";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center postNotificationName:@"DDXNotification" object:observedObject userInfo:user deliverImmediately:YES];
以及接收器中的代码:
NSString *observedObject = @"com.drake.DDX";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(receiveNotification:) name:@"DDXNotification" object:observedObject];
如果我将这段代码添加到我的另一个应用程序中,它似乎可以工作,所以它让我觉得 TextEdit 或 SIMBL 中发生了一些我不知道的事情。
我已经通过在执行方法后立即在 TextEdit 中显示 NSAlert 来检查菜单项是否正确执行了该方法。
任何帮助表示赞赏。
(编辑)我发现,当我发布通知时,如果 userInfo 字典为零,我的应用程序会收到通知,因此我发送的 NSDictionary 或接收器方法签名似乎存在问题。