传递给另一个视图控制器时,值从托管对象中消失

时间:2011-09-04 18:23:38

标签: iphone core-data

我有一个使用核心数据的应用程序有点复杂,除了数据似乎消失的一个实例外,它运行良好。

我有一个托管对象类,pitLayer,有10个属性(字符串和数字),一个视图控制器(viewLayers),它显示属于父对象的所有pitLayer对象,以及一个视图控制器(editLayer),可以编辑(和创建)新的坑层对象。

当我创建一个新的pitLayer,然后返回到表视图时,所有属性都会在表中正确显示。如果我立即返回editLayer,所有值都会正确显示。

如果我然后用editLayer编辑另一个pitLayer,我得到除2,硬度和botttomHardness(两个字符串)之外的所有属性的正确值,其中我仍然从之前编辑的pitLayer中获取值。

如果我第一次使用现有的pitLayer加载editLayer,则硬度和bottomHardness返回nil,所有其他值都返回先前存储的值。

值正在正确存储,并且可以在viewLayers中获取它们并返回正确的值,但是当它们在editLayer中获取时,它们不会返回正确的值。它只有2个属性,其他8个似乎工作正常。

有关调试的建议吗?

这是他们设置的地方:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if(component == kGrainTypeComponent){ layer.grainType = [[grainTypeValues objectAtIndex:row] valueForKey:@"code"]; }else if(component == kSecondGrainTypeComponent){ layer.secondGrainType = [[grainTypeValues objectAtIndex:row] valueForKey:@"code"]; } else if(component == kHardnessComponent){ layer.hardness = [[hardnessValues objectAtIndex:row] valueForKey:@"code"]; } else if(component == kSecondHardnessComponent){ layer.bottomHardness = [[hardnessValues objectAtIndex:row] valueForKey:@"code"]; } [self checkSavable]; }

这是他们以后无法获取的地方:

-(void)resetDefaults{
if(edit == YES && layer != nil){//Edit an existing layer
    if(layer.comments)
        comments.text = layer.comments;
    else
        comments.text = @"";
    if(layer.thickness){
        thicknessSlider.value = [layer.thickness floatValue];
        [self thicknessChanged];
    }
    if(layer.grainSize){
        grainSizeSlider.value = [layer.grainSize floatValue];
        [self grainSizeChanged];
    }else{
        grainSizeSlider.value = 0;
        grainSizeLabel.text = @"";
    }
    if(layer.density){
        densitySlider.value = [layer.density floatValue];
        [self densityChanged];
    }else{
        densitySlider.value = 0;
        densityLabel.text = @"";
    }
    NSString *type = layer.grainType;
    NSString *secondType = layer.secondGrainType;
    NSString *hardness = nil;
    hardness = layer.hardness;
    NSString *bottomHardness = nil;
    bottomHardness = layer.bottomHardness;
    NSString *lwc = layer.liquidWaterContent;
    NSUInteger typeIndex = 0;
    NSUInteger secondTypeIndex = 0;
    NSUInteger hardnessIndex = 0;
    NSUInteger bottomHardnessIndex = 0;
    NSLog(@"type:%@, hardness: %@, bottom hardness: %@", type, hardness, bottomHardness);
    if(type){
        for(int i = 0; i < [grainTypeValues count]; i++){
            if([[[grainTypeValues objectAtIndex:i] valueForKey:@"code"]  isEqualToString: type]){
                typeIndex = i;
                break;
            }
        }
    }
    [picker selectRow:typeIndex inComponent:kGrainTypeComponent animated:NO];
    if(secondType){
        for(int i = 0; i < [grainTypeValues count]; i++){
            if([[[grainTypeValues objectAtIndex:i] valueForKey:@"code"] isEqualToString:secondType]){
                secondTypeIndex = i;
                break;
            }
        }
    }
    [picker selectRow:secondTypeIndex inComponent:kSecondGrainTypeComponent animated:NO];
    if(hardness){
        for(int i = 0; i < [hardnessValues count]; i++){
            if([[[hardnessValues objectAtIndex:i] valueForKey:@"code"] isEqualToString:hardness]){
                hardnessIndex = i;
                break;
            }
        }
    }
    [picker selectRow:hardnessIndex inComponent:kHardnessComponent animated:NO];
    if(bottomHardness){
        for(int i = 0; i < [hardnessValues count]; i++){
            if([[[hardnessValues objectAtIndex:i] valueForKey:@"code"] isEqualToString:bottomHardness]){
                bottomHardnessIndex = i;
                break;
            }
        }
    }
    if(secondHardness)
        [picker selectRow:bottomHardnessIndex inComponent:kSecondHardnessComponent animated:NO];
    if(lwc){
        for(int i = 0; i < [lwcValues count]; i++){
            if([[[lwcValues objectAtIndex:i] valueForKey:@"code"] isEqualToString:lwc]){
                lwcSlider.value = [[[lwcValues objectAtIndex:i] valueForKey:@"value"] floatValue];
                lwcLabel.text = [[lwcValues objectAtIndex:i] valueForKey:@"name"];
                break;
            }
        }
    }else{
        lwcSlider.value = 0;
        lwcLabel.text = @"";
    }
    if(layer.formationDate){
        datePicker.date = layer.formationDate;
        [self dateChanged];
    }else{
        formationDate.text = @"";
        datePicker.date = [NSDate date];
    }

}else{//create a new layer
    comments.text = @"";
    thicknessSlider.value = 0;
    thicknessLabel.text = @"0.0";
    grainSizeSlider.value = 0;
    grainSizeLabel.text = @"";
    densityLabel.text = @"";
    densitySlider.value = 0;
    lwcLabel.text = @"";
    lwcSlider.value = 0;
    formationDate.text = @"";
    datePicker.date = [NSDate date];
    [picker selectRow:0 inComponent:kGrainTypeComponent animated:NO];
    [picker selectRow:0 inComponent:kSecondGrainTypeComponent animated:NO];     
    [picker selectRow:0 inComponent:kHardnessComponent animated:NO];
    if(secondHardness)
        [picker selectRow:0 inComponent:kSecondHardnessComponent animated:NO];
}
thicknessUnitLabel.text = layer.parentPit.depthUnit;
locationUnitLabel.text = layer.parentPit.depthUnit;
densityUnitLabel.text = layer.parentPit.densityUnit;
if([layer.parentPit.densityUnit isEqualToString:@"kg/m3"]){
    densitySlider.maximumValue = 1000;
    densitySliderDecimalCutoff = 0;
}else{
    densitySlider.maximumValue = 100;
    densitySliderDecimalCutoff = 20;
}
topLabel.text = [NSString stringWithFormat:@"%1.1F", [[layer topLocation] floatValue]];
bottomLabel.text = [NSString stringWithFormat:@"%1.1F", [[layer bottomLocation] floatValue]];

}

1 个答案:

答案 0 :(得分:0)

  

值正确存储,并且可以获取它们   viewLayers并返回正确的值...

这表示值正在进入托管对象并被保存到持久性存储中。

  

...但是当他们在editLayer中获取时,他们不会返回正确的   值。

对此唯一的解释是,您将UI元素连接到错误的托管对象,错误的属性或nil对象。

调试需要查看代码。