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

发布于 2024-12-27 06:54:00 字数 2108 浏览 4 评论 0原文

在几个月没有做任何事情之后,我开始重新开始 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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫思念不要吵 2025-01-03 06:54:00

在这种情况下,_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 仪器运行它会告诉你这是否是正在发生的事情。但一般来说,您的应用程序需要对该对象有一个强引用才能使其保留下来。

_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.

☆獨立☆ 2025-01-03 06:54:00

我在 Xamarin 中遇到了这个问题。有一段时间效果很好。然后我向 FinishedLaunching 方法添加了额外的代码,StatusItem 开始消失。我有这段代码生成 StatusItem:

    public override void AwakeFromNib ()
    {
        var statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem (30);
        statusItem.Menu = mainMenu;
        statusItem.Image = NSImage.ImageNamed ("menuicon");
        statusItem.AlternateImage = NSImage.ImageNamed ("menuicon_selected");
        statusItem.HighlightMode = true;
    }

最终,我发现了我的问题。在我的 Xcode 中,我已在 AppDelegate 中声明了此属性,但我没有使用它:

@property(nonatomic, retain) IBOutlet NSStatusItem *statusItem;

当我删除 var 时,StatusItem 继续显示其无限的荣耀:)

    public override void AwakeFromNib ()
    {
        statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem (30);
        statusItem.Menu = mainMenu;
        statusItem.Image = NSImage.ImageNamed ("menuicon");
        statusItem.AlternateImage = NSImage.ImageNamed ("menuicon_selected");
        statusItem.HighlightMode = true;
    }

我不必更改它到(强)。事实上,我尝试过,但复制回 Xamarin Studio 时它并没有持续存在。

I was having this issue in Xamarin. For awhile it worked fine. Then I added extra code to the FinishedLaunching method and the StatusItem started vanishing. I had this code generating the StatusItem:

    public override void AwakeFromNib ()
    {
        var statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem (30);
        statusItem.Menu = mainMenu;
        statusItem.Image = NSImage.ImageNamed ("menuicon");
        statusItem.AlternateImage = NSImage.ImageNamed ("menuicon_selected");
        statusItem.HighlightMode = true;
    }

Eventually, I found my problem. In my Xcode I had declared this property in my AppDelegate but I wasn't using it:

@property(nonatomic, retain) IBOutlet NSStatusItem *statusItem;

When I removed the var the StatusItem continued to show in its infinite glory :)

    public override void AwakeFromNib ()
    {
        statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem (30);
        statusItem.Menu = mainMenu;
        statusItem.Image = NSImage.ImageNamed ("menuicon");
        statusItem.AlternateImage = NSImage.ImageNamed ("menuicon_selected");
        statusItem.HighlightMode = true;
    }

I didn't have to change it to (strong). In fact I tried but it didn't persist when copying back to Xamarin Studio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文