mac status bar application not working

人走茶凉 提交于 2019-12-13 22:23:51

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/21896172/mac-status-bar-application-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!