[英]update UILabel, get value from NSNumber
I am working on an student budget application. 我正在处理学生预算申请。 And when user add new expense, label which represent sum of all expenses, should update, but it doesn't.
并且,当用户添加新费用时,代表所有费用之和的标签应更新,但没有更新。
float currentValue = [self.total floatValue];
self.total = [NSNumber numberWithFloat:(currentValue + amountValue)];
self.label.text = [NSString stringWithFormat:@"Razem: %@", total];
In debuger total has a proper value. 在调试器中, total具有适当的值。
Of course when I type something like this 当然当我输入这样的内容
self.label.text = [NSString stringWithFormat:@"something different"];
label changed his content. 标签更改了他的内容。
EDIT. 编辑。
I've changed code: 我更改了代码:
self.label.text = [NSString stringWithFormat:@"Razem: %@", [total stringValue]];
but it updates only once. 但它仅更新一次。 I've recorded this: YT
我已经记录了: YT
The only change you have to make in your code is to replace 'total' with '[total stringValue]'. 您必须在代码中进行的唯一更改是将“ total”替换为“ [total stringValue]”。
self.label.text = [NSString stringWithFormat:@"Razem: %@", [total stringValue]];
or try by setting label's text as: 或尝试将标签的文本设置为:
[self.label setText:[NSString stringWithFormat:@"Razem: %@", [total stringValue]]];
如果要打印浮点(或双精度),则应使用“%f”而不是“%@”
self.label.text = [NSString stringWithFormat:@"Razem: %f", [self.total floatValue]];
NSNumber
is an object, so %@
will print its description. NSNumber
是一个对象,因此%@
将打印其描述。 To print its value as a float, use floatValue
and %f
. 要将其值打印为浮点型,请使用
floatValue
和%f
。
self.label.text = [NSString stringWithFormat:@"Razem: %f", [total floatValue]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.