Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 与UIPickerView有问题_Ios_Objective C_Arrays_Uipickerview_Uisegmentedcontrol - Fatal编程技术网

Ios 与UIPickerView有问题

Ios 与UIPickerView有问题,ios,objective-c,arrays,uipickerview,uisegmentedcontrol,Ios,Objective C,Arrays,Uipickerview,Uisegmentedcontrol,所以我在使用UIPickerView时遇到了一个奇怪的错误。在开始编写代码之前,对应用程序的基本解释如下: 此应用程序使用UISegmentedControl从几种不同的可能性中进行选择。根据分段控件的选择,UIPickerView将填充不同的数组。选择器视图保证有两个(不多也不少)组件。阵列大小不同,这就是我遇到的问题。我在代码中设置了一些测试,以将值保持在数组的范围内,但有些东西工作不正常 现在进入代码: -(void)pickerView:(UIPickerView *)pickerVie

所以我在使用UIPickerView时遇到了一个奇怪的错误。在开始编写代码之前,对应用程序的基本解释如下:

此应用程序使用UISegmentedControl从几种不同的可能性中进行选择。根据分段控件的选择,UIPickerView将填充不同的数组。选择器视图保证有两个(不多也不少)组件。阵列大小不同,这就是我遇到的问题。我在代码中设置了一些测试,以将值保持在数组的范围内,但有些东西工作不正常

现在进入代码:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSMutableArray *array;
    int x = self.segmentController.selectedSegmentIndex;

    if (x == 0) {
        array = [NSMutableArray arrayWithArray:self.length];
    }
    else if (x == 1) {
        array = [NSMutableArray arrayWithArray:self.time];
    }
    else if (x == 2) {
        array = [NSMutableArray arrayWithArray:self.speed];
    }

    //component == 0 has no problems whatsoever
    if (component == 0) {
        if (row < [array count]){
            self.label1.text = [array objectAtIndex:row];
            self.component0 = row;
        }
        else {
            self.label1.text = [array objectAtIndex:[array count] - 1];
            self.component0 = (NSInteger)([array count] - 1);
        }
    }
    //component == 1 has problems, it always thinks that the row number is less than the array count
    else if (component == 1) {
        if (row < [array count]){
            self.label2.text = [array objectAtIndex:row];
            self.secondComponent = row;
        }
        else {
            self.label2.text = [array objectAtIndex:[array count] - 1];
            self.secondComponent = (NSInteger)([array count] - 1);
        }
    }
    else {
        NSLog(@"ERROR: Components out of range");
    }

    //If I comment out this part, the program works as expected
    if (x == 0) {
        [self convertLength];
    }
    else if (x == 1) {
        [self convertTime];
    }
    else if (x == 2) {
        [self convertSpeed];
    }
}
其他有用信息: 长度数组计数:8 时间数组计数:9 速度阵列计数:8


当我记录变量时,它们的行为符合预期,在我尝试访问所述数组中的任何数据之前,它们将其值更改为在“控制器数组”的范围内,但无论出于何种原因,第二个组件都希望访问不包含在数组中的索引。

如何创建pickerView的内容?您可以将内容设置为最大数组的长度吗?我更新了问题以包含相关代码。
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    int x = 0;

    if (self.segmentController.selectedSegmentIndex == 0) {
        x = [self.length count];
    }
    else if (self.segmentController.selectedSegmentIndex == 1)
    {
        x = [self.time count];
    }
    else if (self.segmentController.selectedSegmentIndex == 2)
    {
        x = [self.speed count];
    }

    return x;
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *string;
    NSArray *array = [[NSArray alloc] init];
    if (self.segmentController.selectedSegmentIndex == 0) {
        array = [NSArray arrayWithArray:self.length];
    }
    else if (self.segmentController.selectedSegmentIndex == 1)
    {
        array = [NSArray arrayWithArray:self.time];
    }
    else if (self.segmentController.selectedSegmentIndex == 2)
    {
        array = [NSArray arrayWithArray:self.speed];
    }
    if (row >= [array count]) {
        string = [array objectAtIndex:[array count] - 1];
        NSLog(@"Out of Range");
    }
    else {
        string = [array objectAtIndex:row];
    }

    return string;
}

- (IBAction)segmentDidChange:(id)sender {
    [self.pickerView reloadAllComponents];
    [self pickerView:self.pickerView didSelectRow:self.component0 inComponent:0];
    [self pickerView:self.pickerView didSelectRow:self.secondComponent inComponent:1];
}