启动系统状态栏在 applicationDidFinishLaunching 中不起作用

发布于 2024-12-21 22:40:06 字数 592 浏览 3 评论 0原文

我的项目是基于文档的,只有将加载状态菜单的代码放在 awakeFromNib 下才会执行。

#import "StatusMenuAppDelegate.h"

@implementation StatusMenuAppDelegate

@synthesize window = _window;

-(void)awakeFromNib {
    myStatusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
statusImage = [NSImage imageNamed:@"statusNorm.png"];
[myStatusItem setImage:statusImage];
[myStatusItem setHighlightMode:YES];
[myStatusItem setMenu:myStatusMenu];
}
@end

但是,在我的测试项目(非基于文档)中,代码在 applicationDidFinishLaunching 中执行得很好。为什么?难道我不想在状态菜单中使用此功能吗?

My project is document-based, and the code to load a status menu is only executed if I put it under awakeFromNib.

#import "StatusMenuAppDelegate.h"

@implementation StatusMenuAppDelegate

@synthesize window = _window;

-(void)awakeFromNib {
    myStatusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
statusImage = [NSImage imageNamed:@"statusNorm.png"];
[myStatusItem setImage:statusImage];
[myStatusItem setHighlightMode:YES];
[myStatusItem setMenu:myStatusMenu];
}
@end

But, in my test project (non-document-based), the code is executed fine in applicationDidFinishLaunching. Why? Wouldn't I want to ideally use this function for my status menu?

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

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

发布评论

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

评论(1

迷迭香的记忆 2024-12-28 22:40:06

applicationDidFinishLaunching: 消息由应用程序对象发送至其委托。它不会发送到任何其他对象。

您当然可以在文档对象中响应该消息,但他们永远不会收到它,因为没有任何东西发送给他们。这就是为什么您在文档对象或不是应用程序委托的任何其他对象中的该方法中执行的任何操作都不会发生。

awakeFromNib 是您可以创建状态项的地方。我可能会在 windowControllerDidLoadNib: 中执行此操作。

这是假设您确实希望每个文档都有一个状态项,这可能没有意义。如果这不是您想要的,您应该将状态项的创建、所有权和管理移至单例对象,该对象应该由应用程序的委托创建(可能在 applicationDidFinishLaunching: 内),并且具有任何所有文档都根据需要与该对象进行交互。

The applicationDidFinishLaunching: message is sent by the application object to its delegate. It is not sent to any other objects.

You can certainly respond to that message in your document objects, but they will never receive it because nothing sends it to them. That's why anything you do in that method in a document object, or any other object that isn't the application's delegate, doesn't happen.

awakeFromNib is one place you could create the status item. I would probably do it in windowControllerDidLoadNib:.

That's assuming you really want to have one status item per document, which probably doesn't make sense. If that's not what you want, you should move creation, ownership, and management of your status item to a singleton object, which should be created—probably within applicationDidFinishLaunching:—by the application's delegate, and have any and all documents interact with that object as needed.

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