1

我想在 OS X 中状态栏菜单文本的左侧添加图标。我找不到在我的代码中实现此功能的示例代码。现在我正在使用代码来设置状态栏的图像(如下)和 IBActions 来调用 AppleScript 文件。

    - (void)awakeFromNib {

    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];

    NSBundle *bundle = [NSBundle mainBundle];

    statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"wifi1" ofType:@"png"]];
    statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"wifi2" ofType:@"png"]];

    [statusItem setImage:statusImage];
    [statusItem setAlternateImage:statusHighlightImage];
    [statusItem setMenu:statusMenu];
    [statusItem setHighlightMode:YES];
}

- (void)dealloc {

    [statusImage release];
    [statusHighlightImage release];
    [super dealloc];
}

- (IBAction)RemoteAppleEvents:(id)sender {

    NSString* path = [[NSBundle mainBundle] pathForResource:@"AppleEvents" ofType:@"scpt"];
    NSURL* url = [NSURL fileURLWithPath:path];NSDictionary* errors = [NSDictionary dictionary];
    NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
    [appleScript executeAndReturnError:nil];
    [appleScript release];
}

- (IBAction)InternetSharing:(id)sender {

    NSString* path = [[NSBundle mainBundle] pathForResource:@"WiFiShare" ofType:@"scpt"];
    NSURL* url = [NSURL fileURLWithPath:path];NSDictionary* errors = [NSDictionary dictionary];
    NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
    [appleScript executeAndReturnError:nil];
    [appleScript release];
}

- (IBAction)BluetoothSharing:(id)sender {

    NSString* path = [[NSBundle mainBundle] pathForResource:@"bluetooth" ofType:@"scpt"];
    NSURL* url = [NSURL fileURLWithPath:path];NSDictionary* errors = [NSDictionary dictionary];
    NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
    [appleScript executeAndReturnError:nil];
    [appleScript release];
}

任何帮助,将不胜感激。谢谢!

更新*

我在 IB 中的下拉菜单

我在 IB 中的下拉菜单.

4

1 回答 1

2

更新的答案:

要像这样在 NSMenuitem 或 NSMenu 中设置图标:

在此处输入图像描述

您只需将图像添加到项目并在“属性检查器”中设置菜单图像

在此处输入图像描述

如果要以编程方式设置图像:

给每个菜单项一个出口,然后使用

设置图像:

正如您对状态栏所做的那样

 bar = [NSStatusBar systemStatusBar];
statusItem =  [bar statusItemWithLength: NSVariableStatusItemLength]  ; 

statusImage = [NSImage imageNamed:@"status.icns"];




statusHighlightImage = [NSImage imageNamed:@"statusHighLight.icns"];

[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];

[statusItem setMenu:_statusMenu];
[statusItem setHighlightMode:YES];

menu1Image = [NSImage imageNamed:@"login.icns"];
menu2Image = [NSImage imageNamed:@"persist.icns"];
menu3Image = [NSImage imageNamed:@"thumbIcon_.png"];

[_MenuItem1 setImage:menu1Image];
[_MenuItem2 setImage:menu2Image];
[_MenuItem3 setImage:menu3Image];

在此处输入图像描述

我的代码也考虑了ARC

于 2013-10-07T12:34:21.620 回答