I am trying to add a variable amount of MenuItem objects to a Menu object on runtime. When my game starts I don’t really know if my menu is having 2 or for example 5 items.
for (MyItem* item in myItemCollection)
{
MenuItemImage* menuItem = [MenuItemImage itemFromNormalImage:@"MenuItem.png" selectedImage:@"MenuItemSelected.png" target:self selector:@selector(options:)];
}
How do I now add the MenuItems to a Menu object. By using addChild I get an exception and I can’t really find a solution online.
Also, a side-question: When I can add my MenuItem’s this way. How can I get my MenuItems back and change the image they are displaying?
From NSMenu:
- (void)addItem:(NSMenuItem *)newItemAssuming you have an NSMenu object to send this message to and the MenuItemImage is a subclass of NSMenuItem.
To get you menu items back, depending on what you know about the item you have:
– itemWithTag:
– itemWithTitle:
– itemAtIndex:
Or you can iterate through the items using:
– numberOfItems
– itemArray
Check out the NSMenuItem documentation for getting/setting the image.
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMenu_Class/Reference/Reference.html
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMenuItem_Class/Reference/Reference.html