菜单栏应用程序 - 这是正确的吗?
我正在尝试制作一个应用程序,当主窗口不可见时,它会在菜单栏中显示一个图标。但是,我不确定这段代码以及它是否可以。我没有收到任何错误,也没有警告,但是在几个应用程序在我的应用程序和另一个应用程序之间来回切换之后。我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果应用程序在您释放
statusItem
变量时崩溃,则分配的menu
属性可能会被过度释放。我不确定这一点,因为我不知道这里的theMenu
变量来自。If the app crashes when you're releasing the
statusItem
variable there's probability that assignedmenu
attribute gets over-released. I'm not sure about this since I don't know heretheMenu
variable is coming from.我只是简单地替换了 [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! :)