繁体   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