UIPicker 多个组件崩溃

发布于 2024-12-10 02:49:25 字数 2398 浏览 1 评论 0原文

我正在尝试使用 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];
    }
}
}

I am trying to create an app using UIPickerView with three components. The first component has 3 rows. The second component has 6 rows and the third component has 12 rows. Every time I scroll beyond row 3 in components 2 or 3 the application crashes and points to the first components array. I'm sure this is easy to fix but I'm lost. Thanks in advance.

-(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];
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦罢 2024-12-17 02:49:26

看起来您缺少一些 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 语句来代替。

It looks like you're missing some if statements to control when each block of code gets called.

You have three separate blocks of code presumably for each component but since there's no condition to control the flow, all the blocks get executed whenever a row in any component is selected. (The third block also has another block inside it--don't know why you've written it like that.)

When you go beyond row 3 in the 2nd and 3rd components, the code for the 1st component runs and crashes when it tries to get the 4th row in the array for component 1 (which doesn't exist).

Since the component indexes are zero-based, the conditions should be something like:

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...
}

You could also use a switch statement instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文