简体   繁体   中英

mac status bar application not working

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?

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

Here's FPAppDelete.h:

#import <Cocoa/Cocoa.h>

@interface FPAppDelegate : NSObject <NSApplicationDelegate>

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

@end

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) 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) 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.

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) Go back to Interface Builder and connect Status Menu to the NSMenu control you've added.

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]];

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.

The status bar is the top-right part of your entire screen . It's where the date and time, Spotlight, etc live in the Menu Bar.

The screenshot you posted is of the title bar of your primary NSWindow .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM