2011-03-24 64 views
1

我正在学生预算申请。而当用户添加新费用时,代表所有费用总和的标签应更新,但不包含更新。更新UILabel,从NSNumber获取价值

float currentValue = [self.total floatValue]; 
self.total = [NSNumber numberWithFloat:(currentValue + amountValue)]; 
self.label.text = [NSString stringWithFormat:@"Razem: %@", total]; 

在debuger 具有适当的值。

当然,当我输入这样的事情

self.label.text = [NSString stringWithFormat:@"something different"]; 

标签改变了他的内容。

编辑。

我已经改变了代码:

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

,但它仅更新一次。我录这一点: YT

回答

7

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

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

或通过设置标签的文本为尝试:

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

编辑。 我改变了代码: \t self.label.text = [NSString stringWithFormat:@“Razem:%@”,[total stringValue]]; 但它只更新一次。我录这一点: [YT] [1] [1]:http://www.youtube.com/watch?v=tEr347-V6WU – milijkovic 2011-03-24 11:35:46

+0

编辑我answer.try这一点,并告诉我结果。 – iPhoneDev 2011-03-24 11:55:49

+0

它没有帮助。结果相同 – milijkovic 2011-03-24 12:50:56

2

如果您想打印一个浮点数(或双),你应该使用“%F”,而不是“%@”

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

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

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