我试图创建一个应用程序使用UIPickerView与三个组件。第一个组件有3行。第二个组件有6行,第三个组件有12行。每次滚动到组件2或3中的第3行之后,应用程序就会崩溃并指向第一个组件数组。我相信这很容易解决,但我迷路了。提前谢谢。
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
{
NSString *resultString0 = [[NSString alloc] initWithFormat:
@"Sensor: %@",
[modelArray objectAtIndex:row]];
modelLabel.text = resultString0;
[resultString0 release];
}
{
NSString *resultString1 = [[NSString alloc] initWithFormat:
@"Pixels: %@",
[memoryArray objectAtIndex:row]];
memoryLabel.text = resultString1;
[resultString1 release];
NSString *firstString = horizFaceRecLabel.text;
float horizontalFace = [[horizontalArray objectAtIndex:row] floatValue];
float requiredFace = 100;
output1 = [firstString floatValue];
output1 = horizontalFace / requiredFace;
horizFaceRecLabel.text = [NSString stringWithFormat:(@"%.1f ft"), output1];
NSString *secondString = horizLicensePlateLabel.text;
float horizontalLicense = [[horizontalArray objectAtIndex:row] floatValue];
float requiredLicense = 45;
output2 = [secondString floatValue];
output2 = horizontalLicense / requiredLicense;
horizLicensePlateLabel.text = [NSString stringWithFormat:(@"%.1f ft"), output2];
NSString *thirdString = horizVisualIdLabel.text;
float horizontalVisual = [[horizontalArray objectAtIndex:row] floatValue];
float requiredVisual = 30;
output3 = [thirdString floatValue];
output3 = horizontalVisual / requiredVisual;
horizVisualIdLabel.text = [NSString stringWithFormat:(@"%.1f ft"), output3];
}
{
NSString *resultString2 = [[NSString alloc] initWithFormat:
@"Lens: %@",
[lensArray objectAtIndex:row]];
lensLabel.text = resultString2;
[resultString2 release];
{
NSString *firstDistString = faceRecLabel.text;
float angle = [[anglesArrayA objectAtIndex:row] floatValue];
float densityOne;
float densityTwo;
densityOne = [firstDistString floatValue];
densityTwo = (output1/2)/(sin(angle/2));
faceRecLabel.text = [NSString stringWithFormat:(@"%.1f ft"), densityTwo];
}
}
}
看起来你缺少一些if
语句来控制何时调用每个代码块
每个组件大概有三个独立的代码块,但由于没有控制流的条件,因此每当选择任何组件中的一行时,所有代码块都会执行。(第三个代码块里面还有另一个代码块——不知道你为什么这样写。)
当您在第二和第三个组件中超过第3行时,第一个组件的代码运行并在试图获取组件1(不存在)的数组中的第4行时崩溃。
由于组件索引是从零开始的,所以条件应该是这样的:
if (component == 0)
{
//code for 1st component here...
}
else if (component == 1)
{
//code for 2nd component here...
}
else if (component == 2)
{
//code for 3rd component here...
}
您也可以使用switch
语句。