开发者

update UILabel, get value from NSNumber

开发者 https://www.devze.com 2023-02-19 05:16 出处:网络
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.

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开发者_C百科


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]]];


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]];


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]];
0

精彩评论

暂无评论...
验证码 换一张
取 消