更新 UILabel,从 NSNumber 获取值

发布于 2024-10-25 08:23:24 字数 677 浏览 2 评论 0原文

我正在处理学生预算申请。当用户添加新费用时,代表所有费用总和的标签应该更新,但事实并非如此。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離殇 2024-11-01 08:23:25

您必须在代码中进行的唯一更改是将“total”替换为“[total stringValue]”。

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

或者尝试将标签的文本设置为:

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

The only change you have to make in your code is to replace 'total' with '[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]]];
风向决定发型 2024-11-01 08:23:25

如果你想打印浮点数(或双精度数),你应该使用“%f”而不是“%@”

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

If you want to print a float (or a double) you should use "%f" and not "%@"

self.label.text = [NSString stringWithFormat:@"Razem: %f", [self.total  floatValue]];
淤浪 2024-11-01 08:23:25

NSNumber 是一个对象,因此 %@ 将打印其描述。要将其值打印为浮点数,请使用 floatValue%f

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

NSNumber is an object, so %@ will print its description. To print its value as a float, use floatValue and %f.

self.label.text = [NSString stringWithFormat:@"Razem: %f", [total floatValue]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文