Search code examples
objective-cxcodecocoanscombobox

Dynamic Data for NSCombobox


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.


Solution

  • 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];