Question

I have an NSComboBox in my mainmenunib file. I have created an outlet of combobox "cb" and made a connection of it with my delegate I also connected delegate and datasource with my delegate.

-(void)applicationDidFinishLaunching:(NSNotification *)aNotification
{    arr=[NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f", nil];
[cb reloadData];
}

-(NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox{
return arr.count;
}

-(id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)loc{
return [arr objectAtIndex:loc];
}

But when I run the application data is not coming in combobox. Please help me out as i am new to cocoa programming. Thanks in advance.

Was it helpful?

Solution

Your approach seems reasonable on the face of it, though using a mutable object as an instance variable is often ill-advised (for reasons wholly unrelated to your issue here, and which we needn't get into at this stage).

There are two things that jump out as possible issues:

1) Are you using ARC? If not, arr is going to disappear from under you because -arrayWithObjects returns an autoreleased object and you have nothing retaining it. If you are using ARC (the default for new project on Lion, I believe), this doesn't apply to you. Plus I would expect you would crash, not just get no data.

2) More likely, you forgot to -setUsesDataSource:YES, which is the flag that tells NSComboBox whether to look at its data source or to use the internal contents approach that @JustinBoo supplied. I believe this defaults to NO, which would cause your exact problem. I don't have Interface Builder in front of me at the moment, but IIRC there is a "uses data source" checkbox that you can check to enable this attribute.

OTHER TIPS

You can add objects using -addItemWithObjectValue to Your NSComboBox like this:

arr = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f", nil];

for (int i = 0; i < [arr count]; ++i)
{
    [cb addItemWithObjectValue:[arr objectAtIndex:i]];
}

You can see NSComboBox Reference for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top