在幾個月沒有處理任何東西之後,我開始回到Cocoa開發中。最初,當我開始使用Snow Leopard和Xcode 3時,我現在正在用Xcode 4.2運行Lion,並且遇到了一些我以前沒有遇到的問題。NSStatusItem在發佈時會短暫出現,但會立即消失
我相信這可能是我以前從未使用ARC的事實,所以我確信我錯過了一些東西。
我試圖創建沒有主窗口或停靠圖標的狀態欄應用程序。當我運行應用程序時,我的應用程序的狀態欄圖標會短暫出現約一秒鐘,但隨後消失。
繼承人我的代碼。
QuickPlusAppDelegate.h
#import <Cocoa/Cocoa.h>
@interface QuickPlusAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (assign) NSStatusItem *statusItem;
@property (weak) IBOutlet NSMenu *statusItemMenu;
@property (strong) NSImage *statusItemIcon;
@property (strong) NSImage *statusItemIconHighlighted;
@property (strong) NSImage *statusItemIconNewNotification;
@end
QuickPlusAppDelegate.m
#import "QuickPlusAppDelegate.h"
@implementation QuickPlusAppDelegate
@synthesize statusItemMenu = _statusItemMenu;
@synthesize window = _window, statusItem = _statusItem;
@synthesize statusItemIcon = _statusItemIcon,
statusItemIconHighlighted = _statusItemIconHighlighted,
statusItemIconNewNotification = _statusItemIconNewNotification;
- (void) awakeFromNib
{
NSBundle *appBundle = [NSBundle mainBundle];
_statusItemIcon = [[NSImage alloc] initWithContentsOfFile:[appBundle pathForResource:@"statusItemIcon" ofType:@"png"]];
_statusItemIconHighlighted = [[NSImage alloc] initWithContentsOfFile:[appBundle pathForResource:@"statusItemIconHighlighted" ofType:@"png"]];
_statusItemIconNewNotification = [[NSImage alloc] initWithContentsOfFile:[appBundle pathForResource:@"statusItemIconNewNotification" ofType:@"png"]];
_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[_statusItem setImage:_statusItemIcon];
[_statusItem setAlternateImage:_statusItemIconHighlighted];
[_statusItem setHighlightMode:YES];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// empty
}
@end
編輯如果你看到什麼錯我的代碼,請讓我知道。我肯定會有一些批評,這樣我才能變得更好。
另一編輯似乎當主窗口本身加載時,狀態欄圖標消失。
對您的代碼的建議:使用[appBundle imageForResource:@「statusItemIcon」]而不是您當前的圖片加載代碼。它應該更快,支持@ 2x圖像透明,支持非PNG沒有代碼更改,並且更容易閱讀:) – 2012-01-15 21:19:26
@Catfish_Man謝謝!這正是我正在尋找的那種批評! – 2012-01-15 21:25:40