Correct way to set goal / action for NSMenuItem in Cocoa?

I'm having some difficulties with the initial Cocoa programming that I am doing.

Essentially, I have an NSStatusBar element with NSMenu as a menu. The menu has one NMMenuItem . In IB, I connected NSMenuItem to NSObject , which itself is set to the ApplicationDelegate class; Then I set the accepted actions to the IBAction method in ApplicationDelegate. I think that everything is connected correctly, except when I run the program and click on a menu item, the IBAction method is not called. I can't seem to figure it out. Here is the relevant code.

Application Delegate h file:

 #import <Cocoa/Cocoa.h> @interface sssAppDelegate : NSObject <NSApplicationDelegate> { IBOutlet NSMenu *statusMenu; NSStatusItem *statusItem; } - (IBAction)showPreferencePanel:(id)sender; @end 

Application Delegation File:

 #import "sssAppDelegate.h" @implementation sssAppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application } -(void)awakeFromNib{ statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain]; [statusItem setMenu:statusMenu]; [statusItem setTitle:@"Status"]; [statusItem setHighlightMode:YES]; } - (IBAction)showPreferencePanel:(id)sender { NSLog(@"Hello World!"); } @end 

As I said, in IB I connected NSMenu to statusMenu in the application’s deletion (thus, the entire menu is displayed under NSStatusBar ), and I connected NSMenuItem to NSMenu in NSObject with the application delegation class and connect it to the showPreferencePanel call, but nothing happens, when i run it !!!

I also tried it programmatically, but cannot get the IBAction method.

Edit: I would add some screenshots to show the installation in IB, but I'm not yet allowed.

The main tip containing the menu added to the NSStatusBar is as follows:

  • NSApplication
  • FR FirstResponder
  • NSApplication Application
  • NSFontManager Font NSFontManager
  • NSMenu Main Menu
    • Menu Item (Preferences) NSMenuItem
  • Sss App Delegate sssAppDelegate

NSMenuItem:

  • Sent Actions - showPreferencePanel ---> Sss App Delegate

Sss App Delegate:

  • Outputs - statusMenu ---> Main Menu
  • Actions Received - showPreferencePanel: ---> Main Item (Preferences)
+7
source share
1 answer

Programmatically, you tried:

 [statusItem setTarget:someTarget]; [statusItem setAction:@selector(someSelector)]; 

It should work.

+1
source

All Articles