2012-08-29 15 views
3

我是一名體驗C/C++程序員,但新的ObjC++。我試圖在Mac OSX項目中捕獲NSWorkspacedidmountnofification。NSWorkspacedidmountNotify /可能通用NSWorkspace

我將我的回調添加到我的應用程序委託界面。

- (void)mediaMounted:(NSNotification *)notification; 

實現包括

- (void)mediaMounted:(NSNotification *)aNotification { 
    NSLog(@"mediaMounted volume change."); 
} 

在我的applicationDidFinishLaunching,我自己添加到通知中心。

NSNotificationCenter* ncenter = [[NSWorkspace sharedWorkspace] notificationCenter]; 
[ncenter addObserver: self 
      selector: @selector(mediaMounted) 
       name: NSWorkspaceDidMountNotification 
       object: nil];  

然而,當我運行和安裝盤,我看到:

2012-08-29 09:52:31.753 OSN[2203:903] -[OSNAppDelegate mediaMounted]: unrecognized selector sent to instance 0x101340af0 
2012-08-29 09:52:31.756 OSN[2203:903] -[OSNAppDelegate mediaMounted]: unrecognized selector sent to instance 0x101340af0 

我證實,實例0x101340af0是我OSNAppDelegate自我,但我不明白還有什麼我必須這樣做選擇器被識別。

回答

1

你的選擇應該是:

mediaMounted:mediaMounted

你的實現需要一個NSNotification作爲參數,而不是任何東西。

if([self respondsToSelector:@selector(mediaMounted)]) 
{ 
    NSLog(@"Good to go"); 
} 

例:

您可以通過驗證您的選擇

// this selector 
@selector(test) 

// will call this method 
- (void)test{ } 

// but this selector, noting the : 
@selector(test:) 

// would call this method 
- (void)test:(id)sender{ } 

你可以閱讀有關選擇here

相關問題