Cocoa 如何在手动使用nsrunlop时接收NSN通知
我可以在GUI程序的应用程序委托中观察NSWorkspaceWillPowerOffNotification通知,但不能在命令行实用程序中观察。你知道为什么下面的方法不起作用吗?还是我应该考虑另一种方法 编辑:是否NSWorkspace的通知中心不适用于无GUI程序?(我已确认“通知中心”不是零)Cocoa 如何在手动使用nsrunlop时接收NSN通知,cocoa,nsnotificationcenter,nsrunloop,Cocoa,Nsnotificationcenter,Nsrunloop,我可以在GUI程序的应用程序委托中观察NSWorkspaceWillPowerOffNotification通知,但不能在命令行实用程序中观察。你知道为什么下面的方法不起作用吗?还是我应该考虑另一种方法 编辑:是否NSWorkspace的通知中心不适用于无GUI程序?(我已确认“通知中心”不是零) 您可以,但对于已启动的进程,请参见:啊,我明白了。是的,是一个发射代理。谢谢你的信息! @interface Helper : NSObject -(void)userLogout:(NSNotifi
您可以,但对于已启动的进程,请参见:啊,我明白了。是的,是一个发射代理。谢谢你的信息!
@interface Helper : NSObject
-(void)userLogout:(NSNotification*)notification;
@end
@implementation Helper
-(void)userLogout:(NSNotification*)notification
{
NSLog(@"Notification Received");
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
Helper* helper = [[Helper alloc] init];
NSNotificationCenter* notificationCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
[notificationCenter addObserver:helper
selector:@selector(userLogout:)
name:NSWorkspaceWillPowerOffNotification
object:nil];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
[runLoop run];
}
return 0;
}