I'm trying to have a dynamic number of NSMenuItems in a Statusbar app. Right now I'm reading in a list in the awakeFromNib
. But this only happens once.
What do I have to do to rebuild my NSMenuItems while the applicaiton is running?
Should it happen in something like -(void)menuNeedsUpdate:(NSMenu *)menu
?
Can somebody give me a push in the right direction please.
Asked
Active
Viewed 228 times
0

fabianmoronzirfas
- 4,091
- 4
- 24
- 41
2 Answers
1
Use – addItem:
– removeItem:
methods.
NSMenuItem *test = [[NSMenuItem alloc] initWithTitle:@"test" action:@selector(test) keyEquivalent:@"a"];
[[StatusItem menu] addItem:test];

Parag Bafna
- 22,812
- 8
- 71
- 144
-
Hi, I'm already adding items this way from a list. My problem is to rebuild the menu when changing that list. I'm trying to run do something in `- (void)menuNeedsUpdate:(NSMenu *)menu {` but it does not get called. You can have a look into my code here https://github.com/fabiantheblind/Shortcuts/blob/master/Shortcuts/tmnAppDelegate.m#L111 – fabianmoronzirfas Jul 05 '13 at 11:47
-
[[StatusItem menu] setDelegate:self]; – Parag Bafna Jul 05 '13 at 11:52
-
Sorry I'm to new to this. I don't understand your hint – fabianmoronzirfas Jul 05 '13 at 12:02
-
set status menu delegate. thn - (void)menuNeedsUpdate:(NSMenu *)menu method will get called. – Parag Bafna Jul 05 '13 at 12:04
-
Hm. That gives me: property 'delegate' not found on object of type 'NSStatusItem *' – fabianmoronzirfas Jul 05 '13 at 12:09
-
[[self.statusBar menu] setDelegate:self]; – Parag Bafna Jul 05 '13 at 12:11
-
Okay. That does compile but I'still don't get anything from menuNeedsUpdate. I need to read some more to understand this – fabianmoronzirfas Jul 05 '13 at 12:16
1
Try setting the NSMenuDelegate
on your header file (the .h file), like this:
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate,NSMenuDelegate>{
}
(or in the header file of the class that you need the selector to be called)

ozmax
- 470
- 6
- 17
-
I think there where several things wrong. I had to add NSMenuDelegate and I did not reference some outlets. I fixed it by snooping around other peoples code. :) Thanks anyway – fabianmoronzirfas Jul 21 '13 at 11:47