0

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.

newbieMACuser
  • 847
  • 1
  • 11
  • 19

2 Answers2

2

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];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • What if I will add more items aside from Apple, Ball, Cat and Doll based on the result in the while-loop? – newbieMACuser Dec 10 '14 at 06:56
  • i'm looking for something that works like array_push() in php (http://www.w3schools.com/php/func_array_push.asp) – newbieMACuser Dec 10 '14 at 07:00
  • 1
    @newbieMACuser see `NSMutableArray`'s methods which begin with "add" and "insert" – justin Dec 10 '14 at 07:10
  • @newbieMACuser: As justin said, you have different methods that you may use, if you are changing the array, then use MutableArray and that will update the combobox... – Anoop Vaidya Dec 10 '14 at 07:16
  • first of all, why you want to loop through each for the array element and add them individually? Since you can add all of them in one shot. – Anoop Vaidya Dec 10 '14 at 07:43
0

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
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56