更新 UILabel,从 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
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须在代码中进行的唯一更改是将“total”替换为“[total stringValue]”。
或者尝试将标签的文本设置为:
The only change you have to make in your code is to replace 'total' with '[total stringValue]'.
or try by setting label's text as:
如果你想打印浮点数(或双精度数),你应该使用“%f”而不是“%@”
If you want to print a float (or a double) you should use "%f" and not "%@"
NSNumber
是一个对象,因此%@
将打印其描述。要将其值打印为浮点数,请使用floatValue
和%f
。NSNumber
is an object, so%@
will print its description. To print its value as a float, usefloatValue
and%f
.