2011-05-30 120 views
0

我有一段應用程序用於將單位從英尺和英寸轉換爲釐米。將UITextField值轉換爲NSString

我接受用戶的腳和英寸通過兩個UITextField輸入工作正常。然後我想對輸入的值進行計算並將結果顯示在下面的標籤中。點擊「轉換」按鈕時,我用於計算的代碼如下所示;

-(IBAction)heighConvertButtonAction:(id)sender { 

    NSString *feetCM = heightFeetField.text; 
    NSString *inchesCM = heightInchesField.text; 

    float fFeet = 30.48 * [feetCM integerValue]; 
    float fInches = 2.54 * [inchesCM integerValue]; 
    float fFeetInches = fFeet + fInches; 
    fFeetInchesResult = [NSString stringWithFormat:@"%", fFeetInches]; 

    heightCMLabel.text = fFeetInchesResult; 

}

它工作在這個意義上,它不產生任何錯誤,但是,當轉換按鈕被觸摸它不會做任何事情。我希望能夠用結果填充'heightCMLabel'標籤。

任何幫助,將不勝感激。

感謝

回答

2
fFeetInchesResult = [NSString stringWithFormat:@"%", fFeetInches]; 

不知道是否可以發佈您的代碼時,但是這不是一個有效的字符串格式化只是一個錯誤。

嘗試:

fFeetInchesResult = [NSString stringWithFormat:@"%f", fFeetInches]; 
+0

這是不是在發佈一個錯誤,我只是很愚蠢的!謝謝馬克,那很好。 – 2011-05-30 16:26:29