Clicking on status item should start action but doesn't
I have a status item and would like for several actions to happen when the user clicks on it. Some online help and documentation has gotten me this far.
The header file has this:
- (IBAction)updateStatusItem:(id)sender;
The class .m file has:
开发者_运维知识库 [statusItem setAction:@selector(updateStatusItem:)];
I've tried it in both applicationDidFinishLaunching and in awakeFromNib.
And also in the m file:
-(IBAction)updateStatusItem:(id)sender{
NSLog(@"updateStatusItem worked");}
From what I can see this makes sense, but whenever I click the status item, the menu just drops down normally and the message doesn't get logged. I thought that perhaps I had to link it somehow in IB, but as far as I can tell there is no representation of the status item in IB, it's all done programatically. I know this can be done but it's not working for me. Can any of you see something wrong?
Thanks for the help.
For an action message to be sent, you need to have somewhere to send it. That means you must also set the target
of the object, which is the object on which you want to call the action selector:
[statusItem setAction:@selector(updateStatusItem:)];
[statusItem setTarget:yourController];
When the action is triggered, this will call the ‑updateStatusItem:
method of your controller object.
However, you cannot have a status item that uses target/action and which also has a menu. If your status item has a menu, then it will always be triggered by clicks on the status item, and the target and action of the status item are simply ignored.
精彩评论