【问题标题】:UIPicker with multiple components crashing多个组件崩溃的 UIPicker
【发布时间】:2011-10-17 01:54:28
【问题描述】:

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

【问题讨论】:

    标签: objective-c xcode xcode4 ios4 uipickerview


    【解决方案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 语句。

    【讨论】:

    • 感谢您帮助 Anna,实际上我最初有 if 语句,它工作正常,但除非我使用实际代码所在的组件,否则不会更新所有字段。例如,当我在三个代码块之间使用 if 语句时,除非我移动组件 1,否则我的 firstString、secondString、thirdString 不会更新。如何在任何组件移动时更新它?
    • 您可以使用selectedRowInComponent 方法获取任何组件中当前选定的行,无论当前更改了哪个组件,都可以随时调用该方法。例如:int currentRowInFirst = [pickerView selectedRowInComponent:0];。您可以将特定于组件的代码放在 if 语句中,并将每次要执行的代码放在 if 块之外并使用 selectedRowInComponent。请注意,如果当前在组件中未选择任何行,则该方法将返回 -1,因此请在使用它来索引数组之前检查它。
    • 只是快速跟进。我使用了 if 语句,后跟 selectedRowInComponent 方法,它似乎也在按我想要的方式工作。再次感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多