NSStatusItem在启动时会短暂出现,但会立即消失

Bra*_*ell 11 cocoa objective-c nsstatusbar automatic-ref-counting

几个月后我没有做任何事情,我开始回到可可开发.最初我开始使用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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

编辑如果您发现我的代码有任何问题,请告诉我.我肯定会批评一些,所以我可以变得更好.

另一个编辑似乎状态栏图标在主窗口本身加载时消失.

ipm*_*mcc 22

在这种情况下,_statusItem将自动释放.

    _statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
Run Code Online (Sandbox Code Playgroud)

这将返回一个自动释放的对象._statusItem只是一个iVar.不仅如此,您还将该属性声明为assign:

@property (assign) NSStatusItem *statusItem;
Run Code Online (Sandbox Code Playgroud)

您可能想要做的是创建属性strong,然后使用属性来设置它,而不是直接设置ivar.像这样:

@property (strong) NSStatusItem *statusItem;
Run Code Online (Sandbox Code Playgroud)

然后:

self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
Run Code Online (Sandbox Code Playgroud)

这将导致statusItem被保留.我打赌现在发生的事情是当自动释放池弹出时它会被释放,然后你的应用程序在下次有任何尝试访问它时崩溃,从而导致它从菜单栏中消失.通过Zombies仪器运行它会告诉你确定这是不是发生了什么.但一般来说,您的应用需要对该对象有强烈的引用才能保持这种状态.