Correct way to use checkmarks in NSMenuItem
I have an NSMenuItem. I want the state
aka value
aka checkmark
of the menuitem to match a boolean that I use in an object.
I'm currently using the "Sent Actions" -> "Selector" outlet. Drag it over to my object, and use the IBAction below:
- (IBAction) setMyGlobalBoolean:(id)sender
{
if(globalBoolean)
{
globalBoolean = FALSE;
[sender setState: NSOffState];
}
else if(!globalBoolean)
{
globalBoolean = TRUE;
[s开发者_Python百科ender setState: NSOnState];
}
}
However, I have a feeling that I should be using the Bindings Inspector
to bind the Value
property to the boolean variable.
The problem is that when I try to do that, I cannot link up my boolean to the value. I can connect the Value to the object in IB. But the "Controller Key" field is then blank and un-editable.
The only way I can get the Controller Key field to work is if I hook the Value field up to the Shared User Defaults Controller
... and I don't know what that is or why/if I should use it.
You've mentioned that you have a global boolean (which suggests it is global to your application) but then you also talk about trying to get access to the boolean via the object.
If you want to store the value of your boolean in NSUserDefaults
then you should be using the Shared Used Defaults Controller
. You have access to the controller key because it is a controller.
If the boolean is a member of your object then you should create accessors so that it can be used with key-value coding.
If the boolean is truly a global then I don't think that you can bind a value to it. I would suggest that you create your "global" boolean in your App Delegate, and then bind your NSMenuItem to the App Delegate. As the App Delegate is not a controller you should only need to use globalBoolean as the Model Key Path.
精彩评论