objective-c - NSStatusItem 在启动时短暂出现,但立即消失

标签 objective-c cocoa automatic-ref-counting nsstatusbar

在几个月没有从事任何工作后,我开始重新投入到 Cocoa 开发中。最初,当我开始使用 Snow Leopard 和 Xcode 3 时。我现在使用 Xcode 4.2 运行 Lion,我遇到了一些以前没有遇到过的问题。

我相信这可能是我以前从未使用过 ARC 的事实,所以我确定我遗漏了一些东西。

我正在尝试创建没有主窗口或停靠栏图标的 Statusbar 应用程序。当我运行该应用程序时,我的应用程序的状态栏图标会短暂出现大约一秒钟,然后消失。

这是我的代码。

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

编辑 如果您发现我的代码有任何问题,请告诉我。我肯定会提出一些批评,这样我才能变得更好。

另一个编辑 当主窗口本身加载时,状态栏图标似乎消失了。

最佳答案

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

    _statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];

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

@property (assign) NSStatusItem *statusItem;

您可能想要在这里做的是使属性 strong 然后,而不是直接设置 ivar,而是使用属性来设置它。所以像这样:

@property (strong) NSStatusItem *statusItem;

然后:

self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];

这将导致 statusItem 被保留。我打赌现在发生的事情是当自动释放池弹出时它被释放,然后你的应用程序在下次任何尝试访问它时崩溃,从而导致它从菜单栏中消失。通过 Zombies instrument 运行它会明确地告诉你这是否是正在发生的事情。但总的来说,您的应用程序需要对该对象有一个强引用才能保留下来。

关于objective-c - NSStatusItem 在启动时短暂出现,但立即消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8873418/

相关文章:

ios - UIPopovercontroller dealloc 在 popover 仍然可见时达到

从 Storyboard重新加载 View 时,以编程方式添加的 iOS subview 将丢失

iphone - 在 float 和 int 值的情况下如何检查 NSArray 对象的数据类型?

objective-c - 具有非 ARC 静态库的启用 ARC 的项目

ios - 为什么我的 AVAudioPlayer 不播放?

cocoa - 获取 NSTextView 的连续拖动事件

cocoa - 是否有用于在 Mac 或 iPhone 上开发音频流客户端的库?

objective-c - 在 OS X 上实现后台应用程序的最佳方式

objective-c - 在 Cocoa 中本地绘制边框和高光

objective-c - 如何加入 [Number numberWithChar : c]'s into an NSString?] 的 NSArray