UIlabel没有以编程方式第二次设置

时间:2014-01-01 13:12:58

标签: ios objective-c ipad cocoa uilabel

我有一个间隔为1秒的计时器和一个计算值的功能,并且每1秒设置一次uilabel。

-(void)StartCostTimer
{
    Global *global = [Global getInstance];
    global.Timer = [NSTimer timerWithTimeInterval:1
                                             target:self
                                           selector:@selector(SetCost:)
                                           userInfo:nil repeats:YES];

    [[NSRunLoop mainRunLoop] addTimer:global.Timer forMode:NSRunLoopCommonModes];
    [[NSRunLoop currentRunLoop] addTimer:global.Timer forMode:NSRunLoopCommonModes];

    [global.Timer fire];
}

这是计算的函数

-(void)SetCost:(NSTimer*)theTimer
{
    Global *global = [Global getInstance];
    double cost,timer_add;

    timer_add = ((double)global.total_interval + / 3600);

    cost = (timer_add * 25);

    [self.label_cost_of_operation setText:[NSString stringWithFormat:@"Cost of Operation: $%f",cost]];

    NSLog(@"Time: %f ----- Cost: %@",timer_add, [self.label_cost_of_operation text]);
}

现在问题就出现了。 当我第一次运行应用程序并且第一次显示视图时,代码正常运行并且标签每1秒更新一次。现在,当我转到另一个屏幕并回到此状态时,我可以看到计时器正在运行,并且还显示了显示iulabel中设置的文本的函数的日志。

例如:

2014-01-01 18:35:18.407 Truck Sheet[8271:a0b] Time: 0.056347 ----- Cost: Cost of Operation: $1.408664
2014-01-01 18:35:19.407 Truck Sheet[8271:a0b] Time: 0.056625 ----- Cost: Cost of Operation: $1.415615
2014-01-01 18:35:20.407 Truck Sheet[8271:a0b] Time: 0.056902 ----- Cost: Cost of Operation: $1.422554
2014-01-01 18:35:21.406 Truck Sheet[8271:a0b] Time: 0.057180 ----- Cost: Cost of Operation: $1.429499
2014-01-01 18:35:22.407 Truck Sheet[8271:a0b] Time: 0.057458 ----- Cost: Cost of Operation: $1.436448
2014-01-01 18:35:23.406 Truck Sheet[8271:a0b] Time: 0.057735 ----- Cost: Cost of Operation: $1.443385
2014-01-01 18:35:24.406 Truck Sheet[8271:a0b] Time: 0.058013 ----- Cost: Cost of Operation: $1.450330
2014-01-01 18:35:25.406 Truck Sheet[8271:a0b] Time: 0.058291 ----- Cost: Cost of Operation: $1.457278
2014-01-01 18:35:26.406 Truck Sheet[8271:a0b] Time: 0.058569 ----- Cost: Cost of Operation: $1.464220
2014-01-01 18:35:27.406 Truck Sheet[8271:a0b] Time: 0.058847 ----- Cost: Cost of Operation: $1.471165
2014-01-01 18:35:28.406 Truck Sheet[8271:a0b] Time: 0.059124 ----- Cost: Cost of Operation: $1.478106
2014-01-01 18:35:29.406 Truck Sheet[8271:a0b] Time: 0.059402 ----- Cost: Cost of Operation: $1.485052
2014-01-01 18:35:30.406 Truck Sheet[8271:a0b] Time: 0.059680 ----- Cost: Cost of Operation: $1.492001
2014-01-01 18:35:31.407 Truck Sheet[8271:a0b] Time: 0.059958 ----- Cost: Cost of Operation: $1.498945
2014-01-01 18:35:32.406 Truck Sheet[8271:a0b] Time: 0.060235 ----- Cost: Cost of Operation: $1.505884
2014-01-01 18:35:33.405 Truck Sheet[8271:a0b] Time: 0.060513 ----- Cost: Cost of Operation: $1.512831

但是iPad上的标签是空白的(我在故事板中设置的值)。这怎么可能?? 我可以看到该值在UILabel中设置,并且NSLog包含来自uiLabel的值而不是计算值。我仍然看不到标签更新.. !!!这似乎是一个愚蠢的问题,但我无法弄明白。

任何帮助将不胜感激。 :)

1 个答案:

答案 0 :(得分:0)

self.label_cost_ofblahblah几乎肯定是零。因为在插座没有连接。在行上方的断点处和调试器中查看selfs变量。

你也应该在camelCase中使用cocoa idiom iVar名称。

如果你有一个名为Global的单例类,那么你最好做一些反对好OOP可可范式的事情......