1

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

3 Answers 3

7

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]]];
4
  • EDIT. I've changed code: self.label.text = [NSString stringWithFormat:@"Razem: %@", [total stringValue]]; but it updates only once. I've recorded this: [YT][1] [1]: youtube.com/watch?v=tEr347-V6WU
    – milijkovic
    Commented Mar 24, 2011 at 11:35
  • it doesn't help. the same result
    – milijkovic
    Commented Mar 24, 2011 at 12:50
  • I created a sample app for this.Its working fine at my end.Just check that whether the label for atorelease.
    – iPhoneDev
    Commented Mar 24, 2011 at 13:16
  • you're right I was allocing UILabel in - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section thx for help!
    – milijkovic
    Commented Mar 24, 2011 at 13:50
2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.