我有一个 ARC 项目,其中包含单击状态栏图标后出现的自定义视图。我是编程新手,所以我从 GitHub 中提取了这个示例项目来启动和运行。该应用程序运行良好,唯一的问题是状态栏项目。我按照我应该的方式设置了 NSStatusItem,但是一旦我调用 setView,图标似乎就被释放了。我可以单击打开应用程序的菜单栏中的空白区域,以便该项目在那里,只是图标丢失了。(图像已确认有效)。我错过了什么?
这是 NSStatusItem 代码:
//
// WOMAppDelegate.m
// PopoverMenulet
//
// Created by Julián Romero on 10/26/11.
// Copyright (c) 2011 Wuonm Web Services S.L. All rights reserved.
//
#import "WOMAppDelegate.h"
#import "WOMMenulet.h"
#import "WOMController.h"
@implementation WOMAppDelegate
@synthesize window = _window;
@synthesize menulet;
@synthesize statusItem;
@synthesize statusImage;
@synthesize controller;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//SET UP NSSTATUSITEM
statusImage = [NSImage imageNamed:@"basket"];
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[self.statusItem setImage:statusImage];
//[self.statusItem setHighlightMode:YES];
[self.statusItem setEnabled:YES];
self.menulet = [[WOMMenulet alloc] init]; /* square item */
self.controller = [[WOMController alloc] init];
self.menulet.delegate = controller;
[self.statusItem setView:menulet];
}
@end
这是引用的菜单代码:
//
// WOMMenulet.m
// PopoverMenulet
//
// Created by Julián Romero on 10/26/11.
// Copyright (c) 2011 Wuonm Web Services S.L. All rights reserved.
//
#import "WOMMenulet.h"
static void *kActiveChangedKVO = &kActiveChangedKVO;
@implementation WOMMenulet
@synthesize delegate;
- (void)setDelegate:(id<WOMMenuletDelegate>)newDelegate
{
[(NSObject *)newDelegate addObserver:self forKeyPath:@"active" options:NSKeyValueObservingOptionNew context:kActiveChangedKVO];
delegate = newDelegate;
}
- (void)mouseDown:(NSEvent *)event {
[self.delegate menuletClicked];
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == kActiveChangedKVO) {
//NSLog(@"%@", change);
[self setNeedsDisplay:YES];
}
}
@end