mac状态栏应用程序无法正常工作 [英] mac status bar application not working

查看:196
本文介绍了mac状态栏应用程序无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注: http://lepture.com/en/2012/create-a-statusbar-app 简单教程,以使基于状态栏的Mac应用程序正常工作,我也引用了Apple的NSStatusItem类引用-并无法弄清楚我做错了什么?

I'm following this: http://lepture.com/en/2012/create-a-statusbar-app simple tutorial to get a Status Bar based Mac app working, I've referenced Apple's NSStatusItem class reference as well - And cannot figure out what I'm doing wrong?

它只是无法正常工作.我的项目使用ARC.

It's just not working. My project uses ARC.

这是FPAppDelete.h:

Here's FPAppDelete.h:

#import <Cocoa/Cocoa.h>

@interface FPAppDelegate : NSObject <NSApplicationDelegate>

@property (weak) IBOutlet NSMenu *statusMenu;
@property (strong, nonatomic) NSStatusItem *statusBar;

@end

这是FPAppDelegate.m:

Here's FPAppDelegate.m:

#import "FPAppDelegate.h"

@implementation FPAppDelegate
@synthesize statusBar = _statusBar;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

- (void) awakeFromNib {
    self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];

    self.statusBar.title = @"G";

    self.statusBar.menu = self.statusMenu;
    self.statusBar.highlightMode = YES;
}

@end

我完全不希望这样,但是运行应用程序时我得到了它,状态栏中没有任何内容

I'm not expecting this at all, but I get this when I run the app, with nothing in my Status Bar

推荐答案

您似乎愿意听.因此,我将快速向您展示如何运行状态应用程序.

It looks like you are willing to listen. So I'll show you quickly how to run a status application.

(1)将NSMenu添加到侧栏(或任何您调用的名称). (请参见下面的屏幕截图.)由您决定是保留还是删除3个常规菜单项.

(1) Add NSMenu to the side bar (or whatever you call). (See the screenshot below.) It's up to you to keep or remove 3 generic menu items.

(2)在AppDelegate.h下添加以下实例变量.

(2) Add the following instance variables under AppDelegate.h.

@interface AppDelegate : NSObject <NSApplicationDelegate> {
    IBOutlet NSMenu *statusMenu;
    NSStatusItem *statusItem;
    NSImage *statusImage;
}

@property (assign) IBOutlet NSWindow *window;
@property (strong) NSMenuItem *menuItem1; // show application

我还将添加一个属性作为示例.

I'll also add a property as an example.

以下代码适用于AppDelegate.m

The following code is for AppDelegate.m

@implementation AppDelegate
@synthesize menuItem1;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [self setMainStatus];
}

- (void)setMainStatus {    
    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
    statusImage = [NSImage imageNamed:@"statusImage"];
    [statusItem setImage:statusImage];
    [statusItem setMenu:statusMenu];

    NSMutableString *menuname1 = [[NSMutableString alloc] initWithString:NSLocalizedString(@"statusMenuShowApplication", @"Show Application Window")];
    menuItem1 = [[NSMenuItem alloc] initWithTitle:menuname1 action:@selector(statusApplicatinClicked:) keyEquivalent:@""];
    [statusMenu addItem:menuItem1];
}

- (void)statusApplicatinClicked:(id)sender {
    [self.window setIsVisible:YES]; 
}

@end

(3)返回Interface Builder,然后将状态菜单"连接到已添加的NSMenu控件.

(3) Go back to Interface Builder and connect Status Menu to the NSMenu control you've added.

setMainStatus方法(或任何您想命名的方法)以编程方式将菜单项添加到状态菜单.首先,您需要创建NSStatusbar,它使用NSImage.此NSImage用于在状态菜单上显示图标.接下来,将菜单项和分隔符添加到状态菜单(在我的情况下为状态菜单).我将menuItem1作为属性,以便应用程序可以启用/禁用它.那只是一个简单的例子.您可以将NSView添加到状态菜单.如果要添加分隔符,可以执行以下操作.

The setMainStatus method (or whatever you want to name) adds menu items to the status menu programatically. First, you need to create NSStatusbar, which takes NSImage. This NSImage is used to show an icon on the status menu. Next, add menu items and separators to the status menu (status Menu in my case). I have menuItem1 as a property so that the application could enable/disable it. That's just a quick example. You can add NSView to the status menu. If you want to add a separator, it can go as follows.

 [statusMenu addItem:[NSMenuItem separatorItem]];

您不需要应用程序窗口即可运行状态菜单应用程序.不过,如果要将状态申请提交到Apple的Mac App Store,则必须首次显示主应用程序窗口.

You don't need an application window to run a status menu application. You have to show the main application window for the first time if you are going to submit your status application to Apple's Mac App Store, though.

这篇关于mac状态栏应用程序无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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