创建状态项 - 图标显示,菜单不显示
在基于文档的项目中,我尝试创建一个状态菜单。我有一个构建状态栏的单例类,并且我从应用程序委托启动它,如您所见。当我运行它时,我没有收到任何错误,但只有状态栏的图像,但没有下拉菜单。我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
myStatusMenu
声明为出口,但从未加载笔尖(或自己为其分配任何内容)。出口无法凭空获得物品;仅当您加载将插座连接到某些东西的笔尖(或自己将某些东西分配给变量,就好像它不是插座一样)时,才会设置插座。您可以通过向
buildStatusItem
添加一行记录myStatusMenu
实例变量的值来证明这一点。我预计它会是nil
。您需要做的是:
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 themyStatusMenu
instance variable. I expect that it will benil
.What you need to do is:
KBStatusMenu
.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 whethergInstance
has already been set and, if so, return that; only if it hasn't should it initialize and returnself
.