Re: Bind checkbox in menu?
Re: Bind checkbox in menu?
- Subject: Re: Bind checkbox in menu?
- From: Sean Murphy <email@hidden>
- Date: Thu, 21 Sep 2006 23:04:56 -0400
On Sep 21, 2006, at 2:58 PM, Trygve Inda wrote:
I have a menu (attached as a submenu to an item in a menubar menu).
I would
like to check one item in this menu based on a binding.
So...
Somevar = 3, the third item in the menu (and only the third item)
is checked
and the others are not.
Is this possible with bindings?
I know you can set a popup menu to a value based on a binding, but
don't
know how to keep a normal menu bound so the binding sets the
checked item.
Hi Trygve,
The checked status of an NSMenuItem can be set up through its "value"
binding (and binding the actual string name of the item is available
under "title," btw).
The binding of NSMenuItem's value should return an integer, which
specifies its state. Three possible integers are recognized: -1,0,1
which correspond to NSMixedState, NSOffState, NSOnState (respectively).
In your case, however, I'm not so sure bindings would be the best
solution to the problem. Since you want a single variable to
indicate which menu item should have the NSOnState, using bindings
could get kind of complicated. What you could do is take advantage
of each menu item's index, as it exists in an NSMenu. Here's a
simple solution:
[[submenu itemAtIndex:(someVar-1)] setState:NSOnState];
So, if someVar = 3, the fourth menu item will be enabled. (someVar
had to be offset, since index counting begins with zero.) Also,
please note that I called the variable "someVar" to make it clear how
your method could be used here. Using names for symbols which make
their purpose very obvious is more consistent with good Cocoa style
(and programming in general).
Hope this helps, and although this way doesn't utilize bindings, it
only took one line of code..
-Sean
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden