NSStatusItem在启动时短暂显示,但会立即消失 [英] NSStatusItem appears briefly on launch, but promptly disappears

查看:463
本文介绍了NSStatusItem在启动时短暂显示,但会立即消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始回到可口可乐开发几个月后没有工作。最初当我开始使用Snow Leopard和Xcode 3.我现在运行Lion与Xcode 4.2,我遇到了一些我没有遇到的问题。



我相信这可能是我从来没有使用过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 / p>

  #importQuickPlusAppDelegate.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:@statusItemIconofType:@png]];
_statusItemIconHighlighted = [[NSImage alloc] initWithContentsOfFile:[appBundle pathForResource:@statusItemIconHighlightedofType:@png]];
_statusItemIconNewNotification = [[NSImage alloc] initWithContentsOfFile:[appBundle pathForResource:@statusItemIconNewNotificationofType:@png]];

_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[_statusItem setImage:_statusItemIcon];
[_statusItem setAlternateImage:_statusItemIconHighlighted];
[_statusItem setHighlightMode:YES];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//空
}

@ end

修改如果您看到我的代码有任何问题,请告诉我。

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

解决方案

_statusItem会在这种情况下自动释放。

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

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

  @property(assign)NSStatusItem * statusItem; 

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

  @property(strong)NSStatusItem * statusItem; 

,然后:

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

这将导致statusItem被保留。我打赌,现在发生的是,它释放时,autorelease池弹出,然后你的应用程序崩溃,下一次任何东西试图访问它,从而导致它从菜单栏中消失。运行它通过僵尸仪器会告诉你肯定如果这是发生了什么。但一般来说,你的应用程序需要有一个强的引用该对象,它坚持。


I'm starting to get back into Cocoa development after not working on anything for a few months. Originally when I started I was using Snow Leopard and Xcode 3. I'm now running Lion with Xcode 4.2 and I'm running into some issues that I hadn't run into before.

I believe it's probably the fact that I've never used ARC before, so I'm sure I'm missing something.

I'm trying to create Statusbar application, without a main window, or dock icon. When I run the application the Statusbar icon for my application appears briefly, for about a second, but then disappears.

Heres my code.

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

Edit If you see anything wrong with my code please let me know. I definitely would some critique so I can get better.

Another Edit It seems as if the Statusbar icon disappears when the main window itself loads.

解决方案

_statusItem will be autoreleased in this case.

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

This returns an autoreleased object. _statusItem is just an iVar. Not only that, but you declare the property as assign:

@property (assign) NSStatusItem *statusItem;

What you probably want to do here is make the property strong and then, instead of setting the ivar directly, use the property to set it. So like this:

@property (strong) NSStatusItem *statusItem;

and then:

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

That will cause the statusItem be retained. I'm betting that what's happening now is that it gets released when the autorelease pool pops, and then your app crashes the next time anything tries to access it, thus causing it to disappear from the menu bar. Running it through the Zombies instrument would tell you for sure if that was what was happening. But in general, your app needs to have a strong reference to that object for it to stick around.

这篇关于NSStatusItem在启动时短暂显示,但会立即消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆