1

I'm new to objective-c programming and I'm trying to make a status bar application right now.

I only know how to set a dropdown menu to show when click on the status bar item.

However, what I want is to show a panel when left clicked and show the menu when right clicked, just like the way Bartender 2 acts.

I've referred this demo but I could hardly figure out what it does.

I use xib to build my UI. I have three .xib files: MainMenu, Preferences and MainPanel.

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property NSStatusItem *statusItem;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "Menu.h" //Menu is a ViewController for Menu.xib

@interface AppDelegate ()

//@property (weak) IBOutlet NSWindow *window; //I don't know what is this for

@end

@implementation AppDelegate

@synthesize statusItem;

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

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

//I initiate my statusItem here
-(void)awakeFromNib{
    self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];

    self.statusItem.title = @"T";

    // you can also set an image
    //self.statusBar.image =

    self.statusItem.highlightMode = YES;

//I tried to use these code to set the left click action
    [statusItem setTarget:self];
    [statusItem setAction:@selector(showMenu:)];
}

-(void)showMenu{
    Menu* menuVC = [[Menu alloc] initWithNibName:@"Menu" bundle:nil];
//Don't know what to do next...
}
@end

I tried to use [menuVC showWindow]; but it is not right.

Wain
  • 118,658
  • 15
  • 128
  • 151
jinglei
  • 3,269
  • 11
  • 27
  • 46

1 Answers1

0

See this post for information about drawing an NSMenu at a given NSPoint (use popUpContextMenu:withEvent:forView: instead of showWindow).

I'll also point out that in the example you linked, the author modularizes his project into different components, following the MVC pattern. There is a component for the menu controller and view (it doesn't look like he just displays an NSMenu), as well as the panel controller and view. You may want to think about how you can organize your project to follow the conventions of MVC.

Community
  • 1
  • 1
Brian Gradin
  • 2,165
  • 1
  • 21
  • 42
  • Yeah, I'm trying to follow the MVC pattern. But I'm not quite familiar with it yet and what I'm working on is the first project I do. What I have now is a `AppDelegate` class and three `xib` files with three `ViewController` connecting to each of them. But I'm confused, which class is to response to my click action? The `AppDelegate` or the viewcontrollers? – jinglei Mar 22 '16 at 04:54