Re: NSMenu and NSMenuItem help
Re: NSMenu and NSMenuItem help
- Subject: Re: NSMenu and NSMenuItem help
- From: Thomas Lachand-Robert <email@hidden>
- Date: Thu, 10 Jan 2002 09:43:23 +0100
Le jeudi 10 janvier 2002, ` 08:53 , Jeff LaMarche a icrit :
I'm trying to get a handle to one of the menu items in my application's
menu. I'm having two problems, first of all, when I get a sub-menu using
any of the itemAt... methods, it gives an uncaught exception when I then
send an itemAt message to the submenu, something like this:
NSMenu *appMenu;
NSMenuItem *item;
appMenu = [[NSApp mainMenu] itemAtIndex:0];
item = (NSMenu *)([appMenu itemAtIndex:1]); <----- Exception raised:
*** -[NSMenuItem itemAtIndex:]: selector not recognized
What is surprising? RTFM. NSMenuItem doesn't respond to itemAtIndex (it
makes sense: a menu item doesn't have pieces inside). You are confusing
with NSMenu. Why do you cast that way to NSMenu* BTW ? Certainly the
compiler has warned you that putting an NSMenu* in an NSMenuItem* is
incorrect: read the warnings, and read the doc, too. Similarly the
assignment to appMenu is incorrect.
If you intend to get the second item in the first menu, you should do that:
NSMenu* appMenu = [NSApp mainMenu]; // this is the main menu
NSMenuItem* firstMenuItem = [appMenu itemAtIndex:0]; // the first menu
item, is a NSMenuItem !!
if ([firstMenuItem hasSubmenu]) { // should be true for the menu items of
the main menu
NSMenu* firstMenu = [firstMenuItem submenu]; // the first menu
NSMenuItem* myItem = [firstMenu itemAtIndex:1]; // the second item in
that menu
....
}
You may shorten it using imbricated statements.
If your program is correct, you don't need any cast and you don't have any
warning from the compiler. If you have some, try to understand them before
asking questions.
Hope this helps,
Thomas Lachand-Robert
********************** email@hidden
<< Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.