Search code examples
objective-cactiontargetnsmenuitemnsmenu

NSMenuItem target and action


I would like to have help about binding a function to NSMenuItem.

I have an array of object Tab and I would like to draw an NSMenuItem for each of the Tab objects. I succeed in drawing and adding the NSMenuItem but I would like that when user clicks on the item, it calls the instance method (void)play of the concerned Tab object. I've already tried with the following code but it seems to not be working because the menu item stays grey.

-(void)buildInterface: (NSMutableArray *) tabArray ;
{
   for (Tab *currentTab in tabArray)
   {
      NSMenuItem *item = [[NSMenuItem alloc] init];
      [item setTitle:@"test"];
      [item setTarget:currentTab];
      [item setAction: @selector(play:)];

      [statusMenu insertItem:item atIndex:1]; 
   } 
}

Here is the Tab.h

#import <Foundation/Foundation.h>
#import "Safari.h"
#import "GoogleChrome.h"

    @interface Tab : NSObject{

        NSString *plistKey; //key pour le dico de la plist
        NSString *navigateur; //type du navigateur
        NSString *titreTab; //titre de la page Web concernée (hors transmission information)
        NSString *idSite; //nom en toute lettre et mis en forme du site
        NSString *URL;  //URL de l'onglet
        NSInteger *tempsChanson; //durée de la chanson courante en seconde 
        NSInteger *tempsPlayed; //temps déjà joué en secondes
        NSInteger *tabIndex; //index de l'onglet
        SafariTab *tabObjectSafari; //objet de l'onglet concerné, safari
        SafariWindow *tabWindowSafari; //fenêtre de l'onglet conderné, safari
        SafariApplication *applicationSafari;
        GoogleChromeTab *tabObjectChrome;
        GoogleChromeWindow *tabWindowChrome;
        GoogleChromeApplication *applicationChrome;

    }
@property(readwrite, copy) NSString *titreTab;
@property(readwrite, copy) NSString *plistKey;
@property(readwrite, copy) NSString *idSite;
@property(readwrite, copy) NSString *URL;
@property(readwrite, copy) NSString *navigateur;
@property(readwrite, retain) SafariTab *tabObjectSafari;
@property(readwrite, retain) SafariWindow *tabWindowSafari;
@property(readwrite, retain) SafariApplication *applicationSafari;
@property(readwrite, retain) GoogleChromeTab *tabObjectChrome;
@property(readwrite, retain) GoogleChromeWindow *tabWindowChrome;
@property(readwrite, retain) GoogleChromeApplication *applicationChrome;

//initialisation d'un onglet Safari
-(id)initwithSafari: (SafariWindow *) passedWindow tab: (SafariTab *)passedTab application:(SafariApplication *)safariApplication dictionnaryKey:(NSString *) plistKey;

//TODO initialisation d'un onglet avec Google Chrome
-(id)initwithChrome: (GoogleChromeWindow *) passedWindow tab: (GoogleChromeTab *)passedTab application:(GoogleChromeApplication *)chromeApplication dictionnaryKey:(NSString *) plistKey;

-(void)play;
+(NSArray *)isControlabe: (SafariTab *) passedTab;

@end

Here is the link to a picture showing the result after calling the function http://imageshack.us/photo/my-images/3/capturedcran20111127205.png/ (I don't have enough reputation sorry).


Solution

  • You're using [item setAction: @selector(play:)]; which requires a function -(void)play:(id)sender;. You should remove the colon in the selector, so that you can use the function -(void)play;.