Getting a particular menu item from MainMenu

Posted by Garry on Stack Overflow See other posts from Stack Overflow or by Garry
Published on 2010-05-19T21:06:07Z Indexed on 2010/05/19 21:10 UTC
Read the original article Hit count: 189

Filed under:
|
|
|

I have a nib (winA.xib) that contains a window. My app delegate contains an NSWindowController subclass called WinAController.

WinAController has a property (NSMenu *mainMenu) that I want to point to the MainMenu. I have set it after I instantiate WinAController with this code:

    WinAController = [[WinAController alloc] initWithWindowNibName:@"WinA"];
    WinAController.mainMenu = [NSApp mainMenu];

I have a menu item underneath the "Window" top-level menu item on MainMenu that invokes the [WinAController showWindow] method and displays WinA. I want to toggle the on/off state of this menu item depending on whether WinA is visible or not. WinAController also has another property (NSMenuItem *myMenuItem).

How can I get a reference to a sub menu of the "Window" top-level menu item. The title of sub menu item I want to get is "Command". I have tried this:

    if (mainMenu != nil) {
    myMenuItem = [mainMenu itemAtIndex:[mainMenu indexOfItemWithTitle:@"Command"]];
}

But it doesn't seem to work.

Where am I going wrong?

Thanks,

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about objective-c