0

我有一个 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
4

3 回答 3

3

setImage 仅在 NSStatusItem 的默认视图或默认视图中有效,一旦您调用- setView:您正在设置要在菜单栏中显示的自定义视图。如果您希望在菜单栏中有图像,则此自定义视图应自行绘制图像。

此信息可以在文档中找到,这里: NSStatusItem 类参考

注意:

讨论 设置自定义视图会覆盖由 NSStatusItem 定义的所有其他外观和行为设置。自定义视图负责绘制自己并提供自己的行为,例如处理鼠标点击和发送动作消息。

于 2013-05-13T12:50:51.540 回答
-1

我回到了 GitHub 存储库中的原始代码,虽然我从其他人那里学到的不是很漂亮,但只需稍加调整即可工作。感谢大家的帮助,我现在知道如何为 V2 做些什么了。

于 2013-05-13T13:29:04.627 回答
-1

此视图将仅绘制您在项目上设置的内容

@interface DDQuickMenuStatusItemView : NSView
@property(weak) NSStatusItem *item;
//...
@end


@implementation DDQuickMenuStatusItemView

//...

- (void)drawRect:(NSRect)dirtyRect {
    NSImage *image = nil;
    if(self.item) {
        [self.item drawStatusBarBackgroundInRect:self.bounds withHighlight:NO];
        image = self.item.image;
    }

    if(image) {
        NSRect r = self.bounds;
        r.size = [image size];
        r = [self.class centerRect:r inRect:self.bounds];
        r = [self centerScanRect:r];
        [image drawInRect:r fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
    }
}

#pragma mark -

+ (CGRect)centerRect:(CGRect)rect inRect:(CGRect)inRect
{
    CGRect result = rect;
    result.origin.x = inRect.origin.x + (inRect.size.width - result.size.width)*0.5f;
    result.origin.y = inRect.origin.y + (inRect.size.height - result.size.height)*0.5f;
    return result;
}

@end
于 2013-05-13T13:17:25.853 回答