1
我想在xcode 4上只做一個簡單的菜單欄應用程序。一切實際上都有效,但是我不明白的是該圖標在菜單欄中出現了兩次。兩個圖標中的一個實際上是工作的,並且帶有工作按鈕的下拉菜單,另一個只是在點擊時改變突出顯示的圖標圖像,並且在被釋放時返回,沒有做任何事情,甚至沒有出現下拉菜單。雙狀態項目圖標出現
這是我發現和測試了代碼:
- (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;
與IBAction爲登錄你好點擊其中一個項目的世界,點擊另一個項目時結束。
我使用了XCode 3的教程,所以它可能是其中一個步驟完成的方式不同,但查看代碼我不知道第二個狀態項的創建位置。
感謝您的幫助。
提問者可能在兩個單獨的nib中也有這個類的兩個對象(也許錯誤地認爲他們會以某種方式加入併成爲一個對象),或者讓這個對象加載它自己的一個筆尖 - 在Cocoa中,一個筆尖的所有者也會發送'awakeFromNib'消息。無論哪種方式,結果都是一樣的,我同意你提出的解決方案。 –
將它移動到appDidFinishLaunching做到了。謝謝! – Elbimio