I have already created a status item for the menu bar but I would like to add a checkbox to make it able to be toggled on and off.
So when the check box is checked the status item is displayed and when the checkbox is not checked it is not displayed.
What code would i need to do this?
First in your controller class create an instance variable to hold the reference to this item:
Then create a method to create this status item, when the box is checked:
Then create a method to remove the item when it is unchecked:
Now tie it all together by creating an action that is called when the checkbox is toggled:
Then create the checkbox in IB and set the action to your
toggleStatusItem:method; make sure that the checkbox is left unchecked.Edit (In response to errors)
As stated above, you need to declare the
NSStatusItemin the interface of the class that you have placed thecreateStatusItemandremoveStatusItemmethods; the reason that this becomes an instance variable rather than one local to thecreateStatusItemmethod is that there is no way to retrieve a pointer to an item that has already been added to the status bar in the Apple menu, and in order to remove the item once the checkbox is unchecked, you must store a pointer to this item. This will also solve your third error.In response to your second error, I was merely demonstrating that if you want to add a menu to your status item when it is clicked, you must add the code for that yourself, retrieving a pointer to an
NSMenu; I was showing how you could then add this menu item to the status bar item, if your pointer was calledmenu, hence my comment next to the line of code.