2012-03-05 30 views
0

如果我的問題是如此愚蠢的問題,我很抱歉提前。 我試圖編寫一個缺乏編程基礎知識的應用程序。 我應用此功能從某人在stackoverflow論壇。然後我編輯了一下。如何在使用後清潔變量或功能

-(void)computeDistanceFrom:(CLLocation *)oldL tO:(CLLocation *)newL { 

    NSLog(@"oldL %@",oldL); 
    NSLog(@"newL %@",newL); 

    CLLocationDistance currentDistance = [oldL distanceFromLocation:newL]; 
    NSLog(@"you have travel=%f",currentDistance); 
    distance = distance + currentDistance; 

    distanceInKm = distance/1000; 
    carbonSaveKg = distanceInKm*0.069565217; 
    fuelSaveLit = distanceInKm*0.073913043; 

    //set string to show 
    distanceLabelValue = [[NSString stringWithFormat:@"%1.3f",distanceInKm]retain]; 
    distanceLabel.text = distanceLabelValue; 
    NSLog(@"distanceFromView=%f",distanceInKm); 

    carbonDioxideLabelValue = [[NSString stringWithFormat:@"%1.3f",carbonSaveKg]retain]; 
    carbonDioxideLabel.text = carbonDioxideLabelValue; 
    NSLog(@"CO2FromView=%f",carbonSaveKg); 

    fuelLabelValue = [[NSString stringWithFormat:@"%1.2f",fuelSaveLit]retain]; 
    fuelLabel.text = fuelLabelValue; 
    NSLog(@"FuelFromView=%f",fuelSaveLit); 

    } 

我的應用程序跟蹤用戶的當前位置。然後它通過標籤顯示距離和一些值。當我離開它包含此功能的看法,我想給變量distanceInKmcarbonSaveKGfuelLit的值設置爲0。

我的問題是,當我回到這一觀點再次,該變量的值仍然像以前一樣。 請幫幫我。

非常感謝您的幫助。

回答

0

插入此方法並在返回到此視圖之前調用它。

- (void)clearLabels { 
    distanceLabel.text = @""; 
    carbonDioxideLabel.text = @""; 
    fuelLabel.text = @""; 
} 
+0

感謝您的回答@Matic。我試過這個函數,它看起來像剛剛清除標籤的函數,但沒有清除保持該值的變量。謝謝。 – 2012-03-06 16:34:34

+0

我不明白你真正想要做什麼..你也可以爲所有值調用distanceLabelValue = @「」和distanceInKm = 0。但也知道這個代碼可能會有內存泄漏,因爲你的字符串被保留了下來,所以一定要在給它們分配另一個值之前調用它們。 – 2012-03-07 07:29:46

+0

我對這些東西很好奇。起初,我沒有保留它們,編譯器返回內存泄漏問題'BAD_EXC_ACCESS'。我保留他們之後,錯誤消失了。 :(更多的,我已經嘗試distanceInKm = 0;但結果仍然是一樣的,我猜想,也許是因爲保留,所以變量仍然保持包含值 – 2012-03-08 00:40:25