2010-07-22 39 views
0

我有一个数组初始化访问目标C嵌套数组元素

- (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文本设置为“项1-2”值,例如。我怎样才能做到这一点?

回答

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

您确实需要让myArray对其他方法可见 - 将它放在类声明中是最简单的方法。不要忘记在dealloc中释放它。

+0

'myArray'必须声明为一个实例变量。如果你不知道该怎么做,请阅读:http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html – bbum 2010-07-22 20:55:13