Question

I have a simple cocoa coredata statusbar application with Xcode 4.6.2. Here in my AppController.h i have

@interface AppController : NSObject
@property NSStatusItem *statusItem;
@property IBOutlet NSMenu *statusMenu;

In my AppController.m:

@synthesize statusItem = statusItem;
@synthesize statusMenu = statusMenu;
-(void)awakeFromNib{
    statusItem = [[NSStatusBar systemStatusBar]statusItemWithLength:NSVariableStatusItemLength];
    statusItem.menu = statusMenu;
}

then, in my AppDelegate.m there is a function:

#import "AppController.h"
-(IBAction)someAction:(id)sender{
    //code to do something
    AppController *x = [[AppController alloc]init];
    [x.statusMenu cancelTracking];
}

I want to close the menu via a button that performs an action inside a custom view of a NSMenuItem (from Connection Inspector->Outlets->view ctrl+drag to the button). I cannot select 2 different sent actions for a NSButton, so i have to close the menu declared in AppController class from the IBAction someAction that is in AppDelegate class. How to do it? The code i've posted doesn't work. Thanks in advance

Was it helpful?

Solution

Your code probably doesn't work because you are initializing a second AppController.

You need a reference to your original AppController in AppDelegate. This can be achieved by using cocoa bindings. In your .XIB file, drag a new blue Object from your Object Library, set it to be a AppController class using the inspector, then control-drag into your AppDelegate header to create a binding. Use that reference and remove the AppController *x = [[AppController alloc] init];.

OTHER TIPS

Maybe try this:

[[NSStatusBar systemStatusBar] removeStatusItem:[GSAppDelegate alloc].statusMenu];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top