多个组件崩溃的UIPicker

时间:2011-10-17 01:54:28

标签: objective-c xcode xcode4 ios4 uipickerview

我正在尝试使用带有三个组件的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];
    }
}
}

1 个答案:

答案 0 :(得分:1)

看起来你缺少一些if语句来控制何时调用每个代码块。

对于每个组件,您可能有三个单独的代码块,但由于没有条件来控制流,因此只要选择了任何组件中的行,就会执行所有块。 (第三个区块里面还有另一个区块 - 不知道为什么你这样编写它。)

当您超越第2和第3个组件中的第3行时,第1个组件的代码在尝试获取组件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语句。