Novice Question: Getting Menu selections

  • I'm a Cocoa novice, converting my aging Carbon application framework
    to Cocoa.  I'm building my interfaces directly, not using IB (the
    framework has it's own builder tools), and using Objective C++.

    In Carbon, my menu items had a command code associated with them,
    which I'd snag when the Carbon Event fired and send off for
    handling.  I'm trying to duplicate that functionality here.

    So, I've created a Menu Controller object:

    #if (UT_TARGET_COCOA)
    @interface MenuController : NSObject
    -(id)init;
    -(void)menuItemSelected:(id)sender;
    @end
    #endif

    And it's implementation:
    @implementation MenuController
    -(id)init
    {
    return [super init];
    }
    -(void)menuItemSelected:(id)sender
    {
    CroPL::CPFGetApplication()->HandleCommand([sender tag]);
    }
    @end

    I create a static instance of it:

    static MenuController* sMenuController = [[MenuController alloc] init];

    And when I create the menus, I set the tag to the "command code" from
    my old architecture, the target to the static menu controller, and
    the action to the MenuItemSelected: message:

    void    InsertItemIntoMenu(platformMenu menu, const UtString& text,
    sInt16 before)
    {
    CFStringRef inText = text.ToCFStringRef();
    NSMenuItem* theItem = [menu insertItemWithTitle: (NSString*)inText
    action: nil keyEquivalent: @"" atIndex: before];
    [theItem setTarget:sMenuController];
    [theItem setAction:@selector(testItemSelected:)];
    CFRelease(inText);
    }

    PlatformMenu here is NSMenu*, and UTString is private string class
    that knows how to convert to CFStringRef (bridged to NSString*).

    This doesn't work.  A breakpoint on the menuItemSelected: method
    never fires, and the debugger console tells me only:
    2007-09-13 22:05:19.269 UnitTest[1908] *** +[NSWindow
    orderedWindows]: selector not recognized
    I've verified that neither the static MenuController nor the created
    item are nil as of when I insert.

    I've been poking at this for days without luck.  I'm probably
    completely missing some important step.  Can anyone take pity on me?

    --Chris
  • Hi Chris,

    Two quick things I see that may help:

    > void  InsertItemIntoMenu(...)
    > {
    ...
    > [theItem setAction:@selector(testItemSelected:)];
    ...
    > }

    testItemSelected: should be menuItemSelected: since that's the name of your
    method.

    And this,

    > UnitTest[1908] *** +[NSWindow orderedWindows]: selector not recognized

    Indicates that you're using the orderedWindows selector incorrectly. It
    appears that you're calling [NSWindow orderedWindows] somewhere, rather than
    [NSApp orderedWindows].

    The declaration for that one is in NSApplication:
    - (NSArray *) orderedWindows;

    ...so, you'd use an instance of NSApplication with that, presumably NSApp.

    Try fixing those things and see if the behavior improves.

    - Dave

    On 9/14/07 1:20 AM, "Christopher Kempke" <ckempkemac...>
    wrote:

    > I'm a Cocoa novice, converting my aging Carbon application framework
    > to Cocoa.  I'm building my interfaces directly, not using IB (the
    > framework has it's own builder tools), and using Objective C++.
    >
    > In Carbon, my menu items had a command code associated with them,
    > which I'd snag when the Carbon Event fired and send off for
    > handling.  I'm trying to duplicate that functionality here.
    >
    > So, I've created a Menu Controller object:
    >
    > #if (UT_TARGET_COCOA)
    > @interface MenuController : NSObject
    > -(id)init;
    > -(void)menuItemSelected:(id)sender;
    > @end
    > #endif
    >
    > And it's implementation:
    > @implementation MenuController
    > -(id)init
    > {
    > return [super init];
    > }
    > -(void)menuItemSelected:(id)sender
    > {
    > CroPL::CPFGetApplication()->HandleCommand([sender tag]);
    > }
    > @end
    >
    > I create a static instance of it:
    >
    > static MenuController* sMenuController = [[MenuController alloc] init];
    >
    > And when I create the menus, I set the tag to the "command code" from
    > my old architecture, the target to the static menu controller, and
    > the action to the MenuItemSelected: message:
    >
    > void InsertItemIntoMenu(platformMenu menu, const UtString& text,
    > sInt16 before)
    > {
    > CFStringRef inText = text.ToCFStringRef();
    > NSMenuItem* theItem = [menu insertItemWithTitle: (NSString*)inText
    > action: nil keyEquivalent: @"" atIndex: before];
    > [theItem setTarget:sMenuController];
    > [theItem setAction:@selector(testItemSelected:)];
    > CFRelease(inText);
    > }
    >
    > PlatformMenu here is NSMenu*, and UTString is private string class
    > that knows how to convert to CFStringRef (bridged to NSString*).
    >
    > This doesn't work.  A breakpoint on the menuItemSelected: method
    > never fires, and the debugger console tells me only:
    > 2007-09-13 22:05:19.269 UnitTest[1908] *** +[NSWindow
    > orderedWindows]: selector not recognized
    > I've verified that neither the static MenuController nor the created
    > item are nil as of when I insert.
    >
    > I've been poking at this for days without luck.  I'm probably
    > completely missing some important step.  Can anyone take pity on me?
    >
    > --Chris
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today