Re: Setting key equivalent for menus depending on window
Re: Setting key equivalent for menus depending on window
- Subject: Re: Setting key equivalent for menus depending on window
- From: Kyle Sluder <email@hidden>
- Date: Sun, 20 Oct 2013 12:02:56 -0700
> On Oct 20, 2013, at 4:21 AM, Martin Hewitson <email@hidden> wrote:
>
>
>> On 20 Oct 2013, at 01:15 am, Kyle Sluder <email@hidden> wrote:
>>
>> Rather than rely on intercepting responder chain-based validation, wouldn't it be much easier and more reliable to make some object the delegate of all of your NSMenus and implement -menuNeedsUpdate:?
>
> But wouldn’t this object then need to know details about the different windows which are presented in the app in order to decide which shortcut key to set?
No. That's what protocols are for, either formal or informal.
@implementation MyAppDelegate
@synthesize _closeMenuItem, _closeAllMenuItem;
- (void)menuNeedsUpdate:(NSMenu *)menu
{
// First, set the default shortcuts in case the main window is unwilling or unable
_closeMenuItem.keyEquivalent = @"w";
_closeAllMenuItem.keyEquivalent = @"";
// Let the main window muck with the key equivalents if appropriate
id mainWindowDelegate = [[NSApp mainWindow] delegate];
if ([delegate respondsToSelector:@selector(updateCloseMenuItem:closeAllMenuItem:)])
[delegate updateCloseMenuItem:_closeMenuItem closeAllMenuItem:_closeAllMenuItem];
}
If you want to be super-fancy, you could take a hint from NSFontManager and send an “update menus” message down the responder chain, but I prefer the simplicity of this approach.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden