访问Objective-C嵌套数组元素

时间:2010-07-22 19:09:06

标签: objective-c nsarray

我有一个数组初始化

- (void) viewDidLoad {
    NSArray *myArray = [NSArray arrayWithObjects:
                   [NSArray arrayWithObjects:@"item 1-1", @"item 1-2", nil],
                   [NSArray arrayWithObjects:@"item 2-1", @"item 2-2", nil],
                   [NSArray arrayWithObjects:@"item 3-1", @"item 3-2", nil],
                   nil];
}

- (IBAction) someButtonPressed {
    NSString *text = // and here I can't figure out how to actually access the value needed
    [someLabel setText:text];
}

例如,我需要将someLabel文本设置为“item 1-2”值。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

[someLabel setText:[[myArray objectAtIndex:0] objectAtIndex:1];

你需要让myArray对其他方法可见 - 将它放在你的类声明中是最简单的方法。不要忘记以dealloc发布它。