objective c - How to enable main menu item "copy"? -
my main menu item "copy" not clickable:
but enable in xcode:
i haven't outlets of main menu items in code. can do?
“enabling menu items” in application menu , pop-up list programming topics says this:
by default, every time user event occurs,
nsmenu
automatically enables , disables each visible menu item. can force menu update usingnsmenu
’supdate
method.
and this:
if menu item’s target not set (that is, if
nil
—typically if menu item connected first responder) ,nsmenu
object not contextual menu,nsmenu
uses responder chain (described in “the responder chain” in cocoa event handling guide) determine target. if there no object in responder chain implements item’s action, item disabled. if there object in responder chain implements item’s action,nsmenu
checks see if object implementsvalidatemenuitem:
orvalidateuserinterfaceitem:
method. if not, menu item enabled. if does, enabled status of menu item determined return value of method.
by default (when create project using “cocoa application” template), copy menu item's target first responder (nil
) , action copy:
. need implement copy:
method on item in responder chain. sufficient enable menu item. if want more precise control of when menu item enabled, can implement validatemenuitem:
check menu item being validated , return yes
or no
appropriate.
for example, application delegate in responder chain. can add method cmappdelegate
:
- (ibaction)copy:(id)sender { nslog(@"%@ %s", self, __func__); }
that should sufficient enable copy menu item. of course, choosing edit > copy log message console. it's write code copies whatever user has selected.
if want more granular control, try giving app delegate outlet connected copy menu item:
@interface appdelegate : nsobject <nsapplicationdelegate> @property (assign) iboutlet nswindow *window; @property (strong) iboutlet nsmenuitem *copymenuitem; @end
hook outlet in mainmenu.xib
. can implement validatemenuitem:
this:
- (bool)validatemenuitem:(nsmenuitem *)menuitem { if (menuitem == self.copymenuitem) { nslog(@"%@ %s %@", self, __func__, menuitem); return [self shouldenablecopymenuitem]; } return no; }
Comments
Post a Comment