菜单栏应用程序 - 这是正确的吗?

发布于 2024-10-21 18:07:00 字数 877 浏览 0 评论 0原文

我正在尝试制作一个应用程序,当主窗口不可见时,它会在菜单栏中显示一个图标。但是,我不确定这段代码以及它是否可以。我没有收到任何错误,也没有警告,但是在几个应用程序在我的应用程序和另一个应用程序之间来回切换之后。我知道 Twitter 或 Safari

程序接收信号: “EXC_BAD_ACCESS”。

这是我的代码:

- (void)applicationDidResignActive:(NSNotification*)aNotification
{
        statusItem = [[[NSStatusBar systemStatusBar] 
                       statusItemWithLength:NSVariableStatusItemLength]
                      retain];
        [statusItem setHighlightMode:YES];
        [statusItem setEnabled:YES];

            //Set menubar item's tooltip
        [statusItem setToolTip:@"Nucleus"];
        [statusItem setMenu:theMenu];
            //Set the menubar item's title
        [statusItem setTitle:[NSString stringWithString:@"N"]]; 

}

- (void)applicationDidBecomeActive:(NSNotification*)aNotification
{
    [statusItem release];
}

I'm trying to make an app, which, when the main window isn't visible, shows an icon in the menubar. However, I'm not sure about this code and whether it's ok. I get no errors and no warnings but after a few app switches back and forth between my app and another eg. Twitter or Safari I get

Program received signal:
“EXC_BAD_ACCESS”.

Here is my code:

- (void)applicationDidResignActive:(NSNotification*)aNotification
{
        statusItem = [[[NSStatusBar systemStatusBar] 
                       statusItemWithLength:NSVariableStatusItemLength]
                      retain];
        [statusItem setHighlightMode:YES];
        [statusItem setEnabled:YES];

            //Set menubar item's tooltip
        [statusItem setToolTip:@"Nucleus"];
        [statusItem setMenu:theMenu];
            //Set the menubar item's title
        [statusItem setTitle:[NSString stringWithString:@"N"]]; 

}

- (void)applicationDidBecomeActive:(NSNotification*)aNotification
{
    [statusItem release];
}

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

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

发布评论

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

评论(2

芸娘子的小脾气 2024-10-28 18:07:00

如果应用程序在您释放 statusItem 变量时崩溃,则分配的 menu 属性可能会被过度释放。我不确定这一点,因为我不知道这里的 theMenu 变量来自。

If the app crashes when you're releasing the statusItem variable there's probability that assigned menu attribute gets over-released. I'm not sure about this since I don't know here theMenu variable is coming from.

远昼 2024-10-28 18:07:00

我只是简单地替换了 [statusItem release];与 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];这真是一种享受。我不想完全释放它,因为我仍然需要它,以防用户稍后切换应用程序。感谢@Kevin Ballard! :)

I simply replaced [statusItem release]; with [[NSStatusBar systemStatusBar] removeStatusItem:statusItem]; and that worked a treat. I don't want to completely release it as I still need it incase the user switches apps later. Thanks to @Kevin Ballard for that! :)

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