以编程方式将项添加到组合框的内部列表中

时间:2011-01-18 02:49:31

标签: objective-c cocoa combobox

所以,尽管Matt在我的上一个问题中给出了慷慨的解释,但我仍然不理解并决定开始一个新项目并使用内部列表。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    codesList = [[NSString alloc] initWithContentsOfFile: @".../.../codelist.txt"];
    namesList = [[NSString alloc] initWithContentsOfFile: @".../.../namelist.txt"];
    codesListArray = [[NSMutableArray alloc]initWithArray:[codesList componentsSeparatedByString:@"\n"]];
    namesListArray = [[NSMutableArray alloc]initWithArray:[namesList componentsSeparatedByString:@"\n"]];
    addTheDash = [[NSString alloc]initWithString:@" - "];
    flossNames = [[NSMutableArray alloc]init];

     [flossNames removeAllObjects];

    for (int n=0; n<=[codesListArray count]; n++){
        NSMutableString *nameBuilder = [[NSMutableString alloc]initWithFormat:@"%@", [codesListArray objectAtIndex:n]];
        [nameBuilder appendString:addTheDash];
        [nameBuilder appendString:[namesListArray objectAtIndex:n]];
        [comboBoz addItemWithObjectValue:[NSMutableString stringWithString:nameBuilder]];
        [nameBuilder release];
    }


}

所以这是我最近的尝试,列表仍未在我的组合框中显示。我尝试在for循环之外使用addItemsWithObjectValues以及此问题的建议: Is this the right way to add items to NSCombobox in Cocoa?

但仍然没有运气。如果您无法分辨,我正在尝试将文件中的两个字符串与它们之间用连字符组合,然后将新字符串放入组合框中。这两个文件中有超过400个代码和匹配的名称,因此手动将它们放入其中将是一项繁重的工作,更不用说,我看不出会导致这个问题的原因。编译器没有显示警告或错误,并且在IB中,我将其设置为使用内部列表,但是当我运行它时,除非我手动执行,否则不会填充列表。

我认为可能导致它的一些事情:

  • 进入 applicationDidFinishLaunching:方法
  • 拥有字符串和数组变量 声明为实例变量 标题(以及@property和 @synth完成了他们)
  • 乱搞使用 appendString多次使用 NSMutableArrays

似乎没有任何事情对我造成这种情况,但也许其他人会知道我不知道的事情。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您是否尝试在调试器下运行此代码并单步执行代码?如果你这样做,我打赌你会发现codesListnamesListcomboBoz是零。

顺便说一下,flossNames没有做任何事情,使用-[NSString stringWithFormat:]可以更简单地完成循环内的部分。

相关问题