2

Greetings I have the following problem trying to set a datasource in an NSComboBox.

This is my custom datasource class:

@interface CComboDatasource : NSObject <NSComboBoxDataSource> {
@private
    NSMutableArray* values;
}
@property (nonatomic,retain) NSMutableArray* values;
-(int)itemCount;

@end


@implementation CComboDatasource
@synthesize values;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        values=[[NSMutableArray alloc] init];

        [values addObject:@"A"];
        [values addObject:@"B"];
        [values addObject:@"C"];
    }

    return self;
}


- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox
{
    return [values count];
}

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

- (void)dealloc
{
    [values release];
    [super dealloc];
}

@end

Later in another file I connect my IBOutlet with my NSComboBox object (c_box) and I set the datasource (CComboDatasource* data_source).

 [c_box setUsesDataSource:TRUE];
 [c_box setDataSource:data_source];
 [c_box setEditable:NO];

After the previous actions nothing is displayed in the combo box, what am I doing wrong?

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
costas
  • 23
  • 1
  • 3

3 Answers3

1

What you have looks basically right to me. I can think of a few things you could try:

1) Try temporarily replacing "return [values count]" with "return 5" and replacing "return [values objectAtIndex:index]" with "return @"arbitraryString"". If "arbitraryString" then shows up in the combobox, you'll know the problem is with the "values" array.

2) Try initializing the "values" array like this:

values = [NSMutableArray array];

(It's a convenience method offered in NSArray.)

If you stick with an alloc-init method, you should make a separate temporary array that way, assign it to "values," then release it. Otherwise, since you've propertized "values" with "retain," you're retaining it twice.

3) Try adding this line at the end of your c_box calls:

[c_box reloadData];

And any time you change the data source array, call this again.

4) I don't see why separating the data source class from the class controlling the combobox should be a problem, but if it's still not working, try making the window/view controller that owns the combobox outlet the class that implements the NSComboBoxDataSource protocol (the numberOfItemsIn and objectValueFor methods), and either put "values" in this controller class or give this class access to "values."

Hope that helps.

Wienke
  • 3,723
  • 27
  • 40
  • Re: #2, note that `values = [NSMutableArray array];` is not the same thing as `self.values = [NSMutableArray array]`. The OP had the correct initialization; yours, as is, could cause a crash. – NSGod Apr 23 '11 at 02:55
  • Thanks for the answer .I tried 1) and 3),4) but nothing happened by NSMutableArray seems to be ok Iam really stuck here and everything seems to be OK I dont know if there is a problem with the IBOutlets-connection with the File Owner – costas Apr 23 '11 at 08:47
  • @costas: You're welcome. Glad you solved the problems with the IB setup. (You might want to check your answer, below, as "accepted" to finish things off.) – Wienke Apr 23 '11 at 13:28
  • 1
    @NSGod: Would omitting "self" here really cause a crash? I actually have been in the habit of using "self," but I've wondered whether it is really necessary if there is no identically named local variable to cause a conflict. – Wienke Apr 23 '11 at 13:35
  • thanks for the help ,I am first time here ,need a change of mentality towards cocoa dev (Iam coming from the .net world) – costas Apr 24 '11 at 17:28
0

I have problem with comboBox:objectValueForItemAtIndex: because I have 10 combo box, every combo box I checking by: if (aComboBox == _myCombo)

8 combo box works fine, but 2 not. I don't know what I'm doing wrong and why others work. I thinking about this problem about 2 weeks. I'm trying to delete and create new with different steps, but nothing help.

The solution is to reloadData before select option in awake from nib.

[_myCombo reloadData];
Darson
  • 26
  • 1
  • 2
0

Ok I found the problem ,in order by the custom datasource class to work u need

  1. Create an NSObject and drag it to your editor
  2. Change the type to your custom datasource class
  3. Declare your Datasource as IBOutlet CustomDatasourceClass* myclass
  4. Connect the Object with the previous outlet
  5. Link your NScomboBox datasource (in IB designer) to the CustomDatasourceClass object
sjngm
  • 12,423
  • 14
  • 84
  • 114
costas
  • 23
  • 1
  • 3