我想帮助将函数绑定到NSMenuItem。
我有一个对象Tab数组,我想为每个Tab对象绘制一个NSMenuItem。我成功绘制并添加了NSMenuItem,但我希望当用户点击该项时,它会调用相关Tab对象的实例方法(void)play
。我已经尝试使用以下代码,但它似乎无法正常工作,因为菜单项保持灰色。
-(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];
}
}
这是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
以下是调用该函数后显示结果的图片的链接 http://imageshack.us/photo/my-images/3/capturedcran20111127205.png/ (我没有足够的声誉对不起)。
答案 0 :(得分:2)
您正在使用需要[item setAction: @selector(play:)];
功能的-(void)play:(id)sender;
。您应该删除选择器中的冒号,以便您可以使用函数-(void)play;
。