【发布时间】:2012-01-15 21:12:25
【问题描述】:
在几个月没有做任何事情之后,我开始重新投入 Cocoa 开发。一开始我使用的是 Snow Leopard 和 Xcode 3。我现在正在使用 Xcode 4.2 运行 Lion,并且遇到了一些我以前没有遇到过的问题。
我相信这可能是因为我以前从未使用过 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无需更改代码,并且更易于阅读:)
-
@Catfish_Man 谢谢!这正是我正在寻找的那种批评!
标签: objective-c cocoa automatic-ref-counting nsstatusbar