Cocoa 出现双状态项图标
我试图在Xcode4上创建一个简单的菜单栏应用程序。实际上一切都正常,但我不明白的是,这个图标在菜单栏中出现了两次。两个图标中的一个实际工作,并提供带有工作按钮的下拉菜单,另一个在单击时仅更改为高亮显示的图标图像,并在释放时返回,不做任何操作,甚至下拉菜单也不会出现 这是我找到并测试的代码:Cocoa 出现双状态项图标,cocoa,menuitem,statusbar,menubar,Cocoa,Menuitem,Statusbar,Menubar,我试图在Xcode4上创建一个简单的菜单栏应用程序。实际上一切都正常,但我不明白的是,这个图标在菜单栏中出现了两次。两个图标中的一个实际工作,并提供带有工作按钮的下拉菜单,另一个在单击时仅更改为高亮显示的图标图像,并在释放时返回,不做任何操作,甚至下拉菜单也不会出现 这是我找到并测试的代码: - (void) awakeFromNib{ //Create the NSStatusBar and set its length statusItem = [[[NSStatusBar systemS
- (void) awakeFromNib{
//Create the NSStatusBar and set its length
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
//Used to detect where the files are
NSBundle *bundle = [NSBundle mainBundle];
//Allocates and loads the images into the application which will be used for the NSStatusItem
statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon" ofType:@"png"]];
statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-alt" ofType:@"png"]];
//Sets the images in the NSStatusItem
[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];
//Tells the NSStatusItem what menu to load
[statusItem setMenu:statusMenu];
//Sets the mouse over text
[statusItem setToolTip:@"My Custom Menu Item"];
//Enables highlighting
[statusItem setHighlightMode:YES];
然后释放图像
- (void) dealloc {
//Releases the 2 images we loaded into memory
[statusImage release];
[statusHighlightImage release];
[super dealloc];
和头文件:
@interface MenuletApp : NSObject <NSApplicationDelegate> {
NSWindow *window;
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
NSImage *statusImage;
NSImage *statusHighlightImage;
@interface MenuletApp:NSObject{
NSWindow*窗口;
IBNSMENU*状态菜单;
NSStatusItem*statusItem;
NSImage*状态图像;
NSImage*状态HighlightImage;
使用iAction在单击其中一个项目时记录Hello World,并在单击另一个项目时终止
我使用了一个针对XCode 3的教程,因此其中一个步骤可能会有所不同,但查看代码时,我不知道第二个状态项是在哪里创建的
感谢您的帮助。是否有可能会调用
-awakeFromNib
两次?(尝试将日志消息放入其中)。也许您的xib文件中有两个此类对象
另外,我建议将此移到。提问者也可能在两个单独的笔尖中有这类的两个对象(可能是因为错误地认为它们会以某种方式合并成为一个对象),或让此对象在Cocoa中加载其自己的nib,nib的所有者也会收到一条
awakeFromNib
消息。无论哪种方式,结果都是相同的,我同意您提出的解决方案。将其移动到appDidFinishLaunching即可。谢谢!