[英]Correct way to use checkmarks in NSMenuItem
I have an NSMenuItem. 我有一个NSMenuItem。 I want the
state
aka value
aka checkmark
of the menuitem to match a boolean that I use in an object. 我希望menuitem的
state
aka value
aka checkmark
与我在对象中使用的布尔值匹配。
I'm currently using the "Sent Actions" -> "Selector" outlet. 我目前正在使用“已发送操作”->“选择器”插座。 Drag it over to my object, and use the IBAction below:
将其拖到我的对象上,并使用下面的IBAction:
- (IBAction) setMyGlobalBoolean:(id)sender
{
if(globalBoolean)
{
globalBoolean = FALSE;
[sender setState: NSOffState];
}
else if(!globalBoolean)
{
globalBoolean = TRUE;
[sender setState: NSOnState];
}
}
However, I have a feeling that I should be using the Bindings Inspector
to bind the Value
property to the boolean variable. 但是,我感觉应该使用
Bindings Inspector
将Value
属性绑定到boolean变量。
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.
我可以将值连接到IB中的对象。 But the "Controller Key" field is then blank and un-editable.
但是,“ Controller Key”字段为空且不可编辑。
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. 我可以使Controller Key字段起作用的唯一方法是,如果将Value字段挂接到
Shared User Defaults Controller
,而我不知道这是什么,也不知道为什么/应该使用它。
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
. 如果要将布尔值存储在
NSUserDefaults
,则应使用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.
我建议您在App Delegate中创建“全局”布尔值,然后将NSMenuItem绑定到App Delegate。 As the App Delegate is not a controller you should only need to use globalBoolean as the Model Key Path.
由于App Delegate不是控制器,因此您只需要使用globalBoolean作为模型密钥路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.