NSStatusItem发布图标 [英] NSStatusItem releases icon

查看:144
本文介绍了NSStatusItem发布图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个涉及自定义视图的ARC项目,该项目在单击状态栏图标后出现.我是编程的新手,所以我从GitHub中拉了此示例项目以启动并运行. 该应用程序运行良好,唯一的问题是状态栏项目.我应该设置NSStatusItem,但是一旦调用setView,该图标似乎就会被释放.我可以在打开应用程序的菜单栏中单击一个空白区域,以便该项目在那里,只是缺少该图标. (确认图像有效).我想念什么?

I have an ARC project involving a custom view that appears after clicking a status bar icon. I'm new to programming, so I pulled this example project from GitHub to get up and running. The app runs fine, the only issue is with the status bar item. I set up the NSStatusItem as I should, but as soon as I call setView, the icon seems to be released. I can click an empty space in the menubar which opens the app so the item is there, it's just that the icon is missing. (Image is confirmed to be valid). What am I missing?

这是NSStatusItem代码:

Here's the NSStatusItem code:

//
//  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

推荐答案

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

the setImage is only valid f or the default view of the NSStatusItem, as soon as you call - setView: you are setting a custom view to be displayed in the menubar. This custom view should draw the image by itself if you so desire to have an image in the menubar.

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

This information can be found in the docs, here: NSStatusItem class reference

请注意:

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

Discussion Setting a custom view overrides all the other appearance and behavior settings defined by NSStatusItem. The custom view is responsible for drawing itself and providing its own behaviors, such as processing mouse clicks and sending action messages.

这篇关于NSStatusItem发布图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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