如何在xcode中使用Objective 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
}
似乎无法获得正确的字符串,因为它最终会产生垃圾变量。
答案 0 :(得分:1)
首先,您需要创建项目列表。 (NSArray的)。
NSArray *items = @[@"Apple", @"Ball", @"Cat", @"Doll"];
删除所有现有项目,默认情况下会将三个项目添加到组合框中。
[self.comboBox removeAllItems];
现在将您的项目添加到组合框中:
[self.comboBox addItemsWithObjectValues:items];
答案 1 :(得分: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
}