views:

185

answers:

2

How would make a Status Item when the actually button is clicked in the Menu Bar not in a drop down menu show or hide a window? Sorry if this is a bit vague.

+4  A: 

To achieve this with NSStatusItem you need to create a custom view and replace the default NSStatusItem view by calling its "setView:" method.

You'll implement code in your custom view to react to mouse clicks by e.g. putting up a window. (You can use a button, or other standard views if it works best for you).

I'll warn you this is a bit tricky to get right. Lots of little nuances e.g. with getting the look of your custom view to look right in the menu bar. But this is the general approach you need to take if you want to override the default menu-prompting status item view.

danielpunkass
Are you sure this is the easiest way?
Joshua
I guess "sure" is a strong word, but I wouldn't have answered it this way if I didn't think this was the only way to do it. Maybe somebody else will offer another opinion.
danielpunkass
Yeh, I'll see if anyone else answers.
Joshua
What code would I need to do this or is there a tutorial on this?
Joshua
+5  A: 

NSStatusItem supports the target/action mechanism like many other controls. I haven't used this myself -- I've only ever used an NSStatusItem with a menu attached -- so I don't know when the message is sent (i.e. when the mouse button is clicked or when it's released). If it doesn't do what you want by default, you would need a custom view like Daniel suggests.

Alex
How would I make it have an action?
Joshua
By setting its action.
Peter Hosey
How would you set it's action? Because you can't see it in IB, so you can't do it the normal way.
Joshua
Peter already told you how: call setTarget: and setAction: on the NSStatusItem to give it the object and selector to call.
Alex