gpt4 book ai didi

cocoa - 出现双状态项目图标

转载 作者:行者123 更新时间:2023-12-03 16:59:23 24 4
gpt4 key购买 nike

我正在尝试在 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 的教程,因此可能其中一个步骤的执行方式有所不同,但查看代码我不知道第二个状态项是在哪里创建的。

感谢您的帮助。

最佳答案

-awakeFromNib 是否可能被调用两次? (尝试在其中放入日志消息)。也许您的 xib 文件中有两个此类的对象?

此外,我建议将其移至 -applicationDidFinishLaunching: .

关于cocoa - 出现双状态项目图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6604634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com