0

xcodeで目的のcとcocoaフレームワークを使用してNSComboBoxデータを動的に追加する方法は?

-(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


 }

ガベージ変数になってしまうため、正しい文字列を取得できないようです。

4

2 に答える 2

2

まず、アイテムのリストを作成する必要があります。(NSArray)。

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

デフォルトで 3 つの項目がコンボ ボックスに追加されるため、既存の項目をすべて削除します。

[self.comboBox removeAllItems];

次に、アイテムをコンボ ボックスに追加します。

[self.comboBox addItemsWithObjectValues:items];
于 2014-12-10T06:53:31.097 に答える
0

このようにしてみてください:-

-(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
}
于 2014-12-10T06:58:39.380 に答える