2011-03-24 63 views
1

我正在學生預算申請。而當用戶添加新費用時,代表所有費用總和的標籤應更新,但不包含更新。更新UILabel,從NSNumber獲取價值

float currentValue = [self.total floatValue]; 
self.total = [NSNumber numberWithFloat:(currentValue + amountValue)]; 
self.label.text = [NSString stringWithFormat:@"Razem: %@", total]; 

在debuger 具有適當的值。

當然,當我輸入這樣的事情

self.label.text = [NSString stringWithFormat:@"something different"]; 

標籤改變了他的內容。

編輯。

我已經改變了代碼:

self.label.text = [NSString stringWithFormat:@"Razem: %@", [total stringValue]]; 

,但它僅更新一次。我錄這一點: YT

回答

7

您必須在您的代碼中進行的唯一更改是將'total'替換爲'[total stringValue]'。

self.label.text = [NSString stringWithFormat:@"Razem: %@", [total stringValue]]; 

或通過設置標籤的文本爲嘗試:

[self.label setText:[NSString stringWithFormat:@"Razem: %@", [total stringValue]]]; 
+0

編輯。 我改變了代碼: \t self.label.text = [NSString stringWithFormat:@「Razem:%@」,[total stringValue]]; 但它只更新一次。我錄這一點: [YT] [1] [1]:http://www.youtube.com/watch?v=tEr347-V6WU – milijkovic 2011-03-24 11:35:46

+0

編輯我answer.try這一點,並告訴我結果。 – iPhoneDev 2011-03-24 11:55:49

+0

它沒有幫助。結果相同 – milijkovic 2011-03-24 12:50:56

2

如果您想打印一個浮點數(或雙),你應該使用「%F」,而不是「%@」

self.label.text = [NSString stringWithFormat:@"Razem: %f", [self.total floatValue]]; 
1

NSNumber是一個對象,所以%@將打印其描述。要將其值作爲浮點值打印,請使用floatValue%f

self.label.text = [NSString stringWithFormat:@"Razem: %f", [total floatValue]];