簡體   English   中英

更新UILabel,從NSNumber獲取值

[英]update UILabel, get value from NSNumber

我正在處理學生預算申請。 並且,當用戶添加新費用時,代表所有費用之和的標簽應更新,但沒有更新。

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

在調試器中, total具有適當的值。

當然當我輸入這樣的內容

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

標簽更改了他的內容。

編輯。

我更改了代碼:

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

但它僅更新一次。 我已經記錄了: YT

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

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

或嘗試將標簽的文本設置為:

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

如果要打印浮點(或雙精度),則應使用“%f”而不是“%@”

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

NSNumber是一個對象,因此%@將打印其描述。 要將其值打印為浮點型,請使用floatValue%f

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM