创建状态项 - 图标显示,菜单不显示

发布于 2024-12-21 23:37:57 字数 1402 浏览 4 评论 0原文

在基于文档的项目中,我尝试创建一个状态菜单。我有一个构建状态栏的单例类,并且我从应用程序委托启动它,如您所见。当我运行它时,我没有收到任何错误,但只有状态栏的图像,但没有下拉菜单。我在 IB 中创建了菜单。我搞砸了什么?

代表

#import "KBAppDelegate.h"
#import "KBStatusMenu.h"

@implementation KBAppDelegate
@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{
    KBStatusMenu *aStatusItem = [[KBStatusMenu alloc] init];
    aStatusItem = [[KBStatusMenu instance] buildStatusItem];
}
@end

.h.m

#import <Foundation/Foundation.h>

@interface KBStatusMenu : NSObject 
{
    NSStatusItem *myStatusItem;
    NSImage *statusImage;
    IBOutlet NSMenu *myStatusMenu;
}

+ (KBStatusMenu *)instance;
- (id)buildStatusItem;

@end

#import "KBStatusMenu.h"

@implementation KBStatusMenu
static KBStatusMenu *gInstance = nil;

+ (KBStatusMenu *)instance 
{
    @synchronized(self) {
        if (gInstance == nil)
            gInstance = [[self alloc] init];
    }

    return(gInstance);
}

- (id)buildStatusItem 
{
    myStatusItem = [[[NSStatusBar systemStatusBar]         statusItemWithLength:NSSquareStatusItemLength] retain];
    statusImage = [NSImage imageNamed:@"statusNormTemplate.png"];
    [myStatusItem setImage:statusImage];
    [myStatusItem setHighlightMode:YES];
    [myStatusItem setMenu:myStatusMenu];
    return myStatusItem;
}

@end

In a document-based project I am trying to create a status menu. I have a singleton class that builds the status bar, and I am initiating it from an application delegate, as you can see. When I run this, I get no errors, but only an image of the status bar, but no menu drops down. I created the menu in IB. What am I messing up?

Delegate

#import "KBAppDelegate.h"
#import "KBStatusMenu.h"

@implementation KBAppDelegate
@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{
    KBStatusMenu *aStatusItem = [[KBStatusMenu alloc] init];
    aStatusItem = [[KBStatusMenu instance] buildStatusItem];
}
@end

.h

#import <Foundation/Foundation.h>

@interface KBStatusMenu : NSObject 
{
    NSStatusItem *myStatusItem;
    NSImage *statusImage;
    IBOutlet NSMenu *myStatusMenu;
}

+ (KBStatusMenu *)instance;
- (id)buildStatusItem;

@end

.m

#import "KBStatusMenu.h"

@implementation KBStatusMenu
static KBStatusMenu *gInstance = nil;

+ (KBStatusMenu *)instance 
{
    @synchronized(self) {
        if (gInstance == nil)
            gInstance = [[self alloc] init];
    }

    return(gInstance);
}

- (id)buildStatusItem 
{
    myStatusItem = [[[NSStatusBar systemStatusBar]         statusItemWithLength:NSSquareStatusItemLength] retain];
    statusImage = [NSImage imageNamed:@"statusNormTemplate.png"];
    [myStatusItem setImage:statusImage];
    [myStatusItem setHighlightMode:YES];
    [myStatusItem setMenu:myStatusMenu];
    return myStatusItem;
}

@end

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

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

发布评论

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

评论(1

人间不值得 2024-12-28 23:37:57

您将 myStatusMenu 声明为出口,但从未加载笔尖(或自己为其分配任何内容)。出口无法凭空获得物品;仅当您加载将插座连接到某些东西的笔尖(或自己将某些东西分配给变量,就好像它不是插座一样)时,才会设置插座。

您可以通过向 buildStatusItem 添加一行记录 myStatusMenu 实例变量的值来证明这一点。我预计它会是nil

您需要做的是:

  1. 创建一个笔尖来包含状态项的菜单。
  2. 将文件所有者的类设置为KBStatusMenu
  3. 在 KBStatusMenu 中,实现 init 来加载刚刚创建的笔尖。

然后,当您到达 buildStatusItem 时,加载笔尖将设置插座,并且您将有一个可提供状态项的菜单。

我建议仅创建一个 KBStatusMenu 实例。在这种情况下,我建议强制执行单例: init 应该测试 gInstance 是否已设置,如果是,则返回;只有当它没有发生时,它才应该初始化并返回<​​code>self。

You declared myStatusMenu as an outlet, but never loaded a nib (or assigned anything to it yourself). An outlet cannot get objects out of nowhere; the outlet is set only when you load a nib that has the outlet connected to something (or assign something to the variable yourself, as if it weren't an outlet).

You can prove this by adding a line to buildStatusItem that logs the value of the myStatusMenu instance variable. I expect that it will be nil.

What you need to do is:

  1. Create a nib to contain the status item's menu.
  2. Set the class of the File's Owner to KBStatusMenu.
  3. In KBStatusMenu, implement init to load the nib you just created.

Then, by the time you reach buildStatusItem, loading the nib will have set the outlet, and you will have a menu to give to your status item.

I would recommend only creating one KBStatusMenu instance. In this case, I recommend enforcing the singleton: init should test whether gInstance has already been set and, if so, return that; only if it hasn't should it initialize and return self.

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