创建状态项 - 图标显示,菜单不显示 [英] Creating status item - icon shows up, menu doesn't

查看:147
本文介绍了创建状态项 - 图标显示,菜单不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于文档的项目中,我尝试创建状态菜单。我有一个单例类构建状态栏,我从一个应用程序委托启动它,你可以看到。当我运行这个,我没有得到错误,但只有状态栏的图像,但没有菜单下降。我在IB中创建了菜单。我在搞什么?

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?

#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



.h

#import <Foundation/Foundation.h>

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

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

@end



.m



.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


推荐答案

您已将 myStatusMenu 声明为插座,但从未加载nib(或自行分配任何内容)。一个出口不能得到物体的无处;只有当您加载一个具有连接到某个东西的出口(或者自己分配东西给变量,如同它不是一个出口)时,出口才会被设置。

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

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

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.

你需要做的是:


  1. 创建一个nib以包含状态项的菜单。

  2. 将文件所有者的类设置为 KBStatusMenu

  3. 在KBStatusMenu中,实现 init 加载刚刚创建的nib。
  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.

然后,当您到达 buildStatusItem 时,加载nib将设置插座,

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.

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

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.

这篇关于创建状态项 - 图标显示,菜单不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆