出现双状态项目图标
我正在尝试在 xcode 4 上制作一个简单的菜单栏应用程序。一切实际上都有效,但我不明白的是该图标在菜单栏中出现了两次。两个图标之一实际上可以工作,并提供带有工作按钮的下拉菜单,另一个只是在单击时更改为突出显示的图标图像,并在释放时返回,不执行任何操作,甚至没有出现下拉菜单。
这是我找到并测试的代码:
- (void) awakeFromNib{
//Create the NSStatusBar and set its length
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
//Used to detect where the files are
NSBundle *bundle = [NSBundle mainBundle];
//Allocates and loads the images into the application which will be used for the NSStatusItem
statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon" ofType:@"png"]];
statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-alt" ofType:@"png"]];
//Sets the images in the NSStatusItem
[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];
//Tells the NSStatusItem what menu to load
[statusItem setMenu:statusMenu];
//Sets the mouse over text
[statusItem setToolTip:@"My Custom Menu Item"];
//Enables highlighting
[statusItem setHighlightMode:YES];
然后释放图像
- (void) dealloc {
//Releases the 2 images we loaded into memory
[statusImage release];
[statusHighlightImage release];
[super dealloc];
和头文件:
@interface MenuletApp : NSObject <NSApplicationDelegate> {
NSWindow *window;
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
NSImage *statusImage;
NSImage *statusHighlightImage;
使用 IBAction 在单击其中一项时记录 Hello World,并在单击另一项时终止。
我使用了针对 XCode 3 的教程,因此可能其中一个步骤的执行方式有所不同,但查看代码我不知道第二个状态项是在哪里创建的。
感谢您的帮助。
I'm trying to make a simple menu bar only application on xcode 4. Everything actually works, but what I don't understand is that the icon is appearing twice in the menubar. One of the two icons actually works and gives the drop down menu with the working buttons, the other just changes to the highlighted icon images while clicked and goes back when released, without doing anything, not even the drop down menu appears.
This is the code I found and tested out:
- (void) awakeFromNib{
//Create the NSStatusBar and set its length
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
//Used to detect where the files are
NSBundle *bundle = [NSBundle mainBundle];
//Allocates and loads the images into the application which will be used for the NSStatusItem
statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon" ofType:@"png"]];
statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"icon-alt" ofType:@"png"]];
//Sets the images in the NSStatusItem
[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];
//Tells the NSStatusItem what menu to load
[statusItem setMenu:statusMenu];
//Sets the mouse over text
[statusItem setToolTip:@"My Custom Menu Item"];
//Enables highlighting
[statusItem setHighlightMode:YES];
then release the images
- (void) dealloc {
//Releases the 2 images we loaded into memory
[statusImage release];
[statusHighlightImage release];
[super dealloc];
and the header file:
@interface MenuletApp : NSObject <NSApplicationDelegate> {
NSWindow *window;
IBOutlet NSMenu *statusMenu;
NSStatusItem *statusItem;
NSImage *statusImage;
NSImage *statusHighlightImage;
with an IBAction to log Hello World when one of the items is clicked, and to terminate when the other is clicked.
I used a tutorial meant for XCode 3, so it might be that one of the steps is done differently, but looking at the code I have no idea where the second status item is getting created.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-awakeFromNib
是否有可能被调用两次? (尝试在其中放入日志消息)。也许您的 xib 文件中有两个此类的对象?另外,我建议将其移至
-applicationDidFinishLaunching:
。Is it possible that
-awakeFromNib
is getting called twice? (Try putting a log message in there). Perhaps you have two objects of this class in your xib file?Also, I'd recommend moving this to
-applicationDidFinishLaunching:
.