Re: Frustrating NSMenu problem
Re: Frustrating NSMenu problem
- Subject: Re: Frustrating NSMenu problem
- From: April Gendill <email@hidden>
- Date: Tue, 26 Aug 2003 23:45:46 -0700
2 things
First I found it easier to build the menu completely in the program
(save it's instance which was created in IB). In other words I inserted
an empty popup in the view, in this case a popupbutton but since I
received the exact same error and for roughly the same reason... I will
share my expereince. Below is a snippette of code from the applications
that is credited with the current bruise on my forehead.
Second thing, I know others have a great time with insertMenuItem :
atIndex (i hope i got that right) but I hate it. I am either not smart
enough or knowlegable enough to use it. I learned with flash
actionscript to be as vague as possible. the more specific you are, the
more chance there is to make a mistake.
Now I know popupbuttons are not menus by any stretch, but they have
some things in common.
-(NSArray *)makeMenuNameArray{
dataModel =[[XSDocClass alloc]init];
int i =0;
NSMutableArray * menu=[[NSMutableArray alloc]init];
NSDictionary *userD=[dataModel getUserPreset];
//gather the items from a dictionary in this case, on disk
while (i < [userD count]){
//Step through the main dictionary
NSString * index;
NSString * menuTitle;
index=[NSString stringWithFormat:@"%i",i];
//since I keyed each dictionary of items with strings ( @"0", @"1",
etc)
//I had to convert my counter to a string
NSDictionary * current=[userD objectForKey:index];
menuTitle=[current objectForKey:@"Preset Title"];
//got the title
[menu addObject:menuTitle];
//obviously stuck it in the array
i++;
}
return menu;
//returns the array to awakefromnib in this case.
}
-(void)insertUserItemsInPopUp:(NSMutableArray *)theMenus{
[chooseWM removeItemAtIndex:0];
//first off, since you HAVE to have something in the 0 spot,
//I had to yank it outta there
[chooseWM addItemsWithTitles:theMenus];
//Nifty little method available in popupbutton class,
//that lets me add an array to the menu so I don't
//have to futz with counting.
//In this case I do not need to know the index
//but since the index of the menu and the index
//of the array of information that corrisponds
//are the same... I can still have it perform the
//correct method or action by it's index. I just
//actually do not know or care what that
//index is.
}
/* excerpt from awakeFromNib
NSString *lp=[defaults objectForKey:@"Last Preset"];
int testMenu =[lp intValue];
if (testMenu>=[menuDict count]){
lp=@"0";
//this is a safety net, that keeps the selection
//from being out of range.
[defaults setObject:@"0" forKey:@"Last Preset"];
// some of this I am sure was not needed but I'm still
code cleaning.
}
//
if([lp length]==0){
//i have terrible luck with isEqualTo, so when testing for a
//null string I AWLAYS go from length. I know if length is
//0 it's null and thus invalid. which causes a cool error all
//it's own.
lp=@"0";
}
//ok well now we have the information and it's time to send
it to the menu
NSMutableArray * menu=[[NSMutableArray alloc]init];
[menu addObjectsFromArray:[self makeMenuNameArray]];
//stick the menu names into an array
[self insertUserItemsInPopUp:menu];
//this sticks the array into the menu
[chooseWM selectItemAtIndex:[lp intValue]];
//not relevant but this sets the popup to
//it's previous selection.
P.S. I know I know. I need to use autorelease on the dictionaries and
the class instance. but it's in beta.
and since all of this stuff only happens at launch its not like
Any way, I do hope this offers some help.
On Tuesday, August 26, 2003, at 07:08 AM, Adam Atlas wrote:
>
I'm trying to programatically add a new menu to the main menu. I get
>
the menu from a nib, and then in my code I create a wrapper NSMenuItem
>
with the menu as a submenu, and then I try to add it to [NSApp
>
mainMenu].
>
>
I get these console messages:
>
*** Assertion failure in -[NSMenu insertItem:atIndex:],
>
Menus.subproj/NSMenu.m:455
>
Invalid parameter not satisfying: (index >= 0) && (index <=
>
(_itemArray ? CFArrayGetCount(_itemArray) : 0))
>
>
This would seem to imply that there a negative number of menus in the
>
main menu, which there obviously are not.
>
>
Should I be doing anything differently in adding a menu to the main
>
menu? What am I doing wrong?
>
>
-- Adam Atlas
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.