Dynamic Data for NSCombobox

1.4k Views Asked by At

How to dynamically add NSComboBox data using objective c and cocoa framework in xcode?

-(void)awakeFromNib
{
    NSLog(@"View controller instance with view: %@", self.view);


    char* data = getData(); // I will be using data to populate records below


    // Setup combo box with data from getData() instead of dummy apple, bag, cat, dog
    self.myRecords = @[@“apple”, @“bag”, @“cat”, @“dog"];
    [self.myRecordsCombo addItemsWithObjectValues:self.myRecords];


 }

 // C Method
 int
 getData()
 {
     char name[128];
     NSString *str;

    while(/*traverse through data for combo box */){
        NSString *tempName = [NSString stringWithFormat:@"%c", name];         
        str = [str stringByAppendingString:tempName];

        ....
    }
    NSLog(str); //will be passed to awakeFromNib and populate to combo box


 }

Can't seem to get the right strings as it will end up with garbage variables.

2

There are 2 best solutions below

5
On BEST ANSWER

First you need to create the list of items. (NSArray).

NSArray *items = @[@"Apple", @"Ball", @"Cat", @"Doll"];

Remove all existing items as by default three items gets added to combo box.

[self.comboBox removeAllItems];

Now add your items to the combo box:

[self.comboBox addItemsWithObjectValues:items];
4
On

Try like this:-

-(void)someMethod{
    [self.comboBox removeAllItems];
    yourArr=@[@"Item1,Item2,Item3,Item4"];//Assuming some values
    NSUInteger i=0;
    while (i!=yourArr.count)
    {
       //Below you are sending data to the another method which will populate the combo box
        NSLog(@"%@",yourArr[i]);
        [self yourMethod:yourArr[i]];
        i++;
    }
  }

 //Below is your different method
-(void)yourMethod:(NSString*)yourStr
{
 [self.comboBox addItemWithObjectValue:[NSString stringWithFormat:@"%@",yourStr]];
}


//After seeing your question below is the Conversion from C to Objective-C

-(void)someMethod
{
    NSArray *arr1=[self.comboBox.objectValues[0] componentsSeparatedByString:@","];
       NSUInteger i=0;
    NSString *str;


    while(i<arr1.count-1){
        i++;
            NSLog(@"%@",arr1[i]);
        NSString *tempName = [NSString stringWithFormat:@"%@", arr1[i]];
        str = [str stringByAppendingString:tempName];
    }
    NSLog(@"%@",str); //will be passed
}