使用键和值绑定UIPickerView

时间:2012-04-06 16:21:00

标签: uipickerview ipad

我正在研究UIPickerView。我能够使用以下代码绑定并获取所选值

pickerItems = [[NSArray alloc] initWithObjects:
               [[[Company alloc] initWithName:@"Alabama" value:@"AL"] autorelease],
               [[[Company alloc] initWithName:@"Alaska" value:@"AK" ] autorelease],
               [[[Company alloc] initWithName:@"Arizona" value:@"AZ"] autorelease],
               [[[Company alloc] initWithName:@"California" value:@"CA"] autorelease],
               [[[Company alloc] initWithName:@"Wyoming" value:@"WY"] autorelease], nil];

但我正在使用网络服务,从中获得密钥和价值。我希望他们使用带键和值的循环进行绑定。

如果我使用下面的代码我得到例外'线程1:程序接收信号:“EXC_BAD_ACCESS”'

NSMutableArray *restArray = [[NSMutableArray alloc] initWithCapacity:5];
for (int i = 0; i <= 5; i++) {
    NSString *key = [NSString stringWithFormat:@" Company %d ", i]; 
    NSString *val = [NSString stringWithFormat:@" Company %d ", i]; 
    Company *rest = [[Company alloc] initWithName:key value:val];
    NSLog(@"restArray=%@ (%@)", restArray, NSStringFromClass([restArray class]));
    [restArray addObject:rest];
    [rest release];
}
pickerItems = restArray;
[restArray release];

提前致谢。

0 个答案:

没有答案