简体   繁体   English

用于Objective-C的计时器输出

[英]Timer Outputs for Objective-C

This question is a branch off of my previous question from earlier today, but is still a new question. 这个问题是从我以前的一个分支问题,从今天早些时候,但仍然是一个新问题。 I'm having issues with a timer loop that I declared in the .m file: 我在.m文件中声明的计时器循环有问题:

// battery level connection timer
- (void)batteryLevelTimerRun
{
    batteryLevelSecondsCount = batteryLevelSecondsCount - 1;
    int batteryLevelProgress = batteryLevelSecondsCount;

    NSString *timerOutput = [NSString stringWithFormat:@"Battery Level: %d%%", batteryLevelProgress];
    batteryLevelLabel.text = timerOutput;

    float batteryLevelProgressFloat = batteryLevelSecondsCount / 100;
    [batteryLevel setProgress:batteryLevelProgressFloat animated:YES];

    if (batteryLevelSecondsCount == 20)
    {
        batteryLevelLabel.textColor = [UIColor orangeColor];
    }
    else if (batteryLevelSecondsCount == 10)
    {
        batteryLevelLabel.textColor = [UIColor redColor];
    }
    else if (batteryLevelSecondsCount == 0)
    {
        [batteryLevelTimer invalidate];
        batteryLevelTimer = nil;
    }
}

// battery level connection timer
- (void)batteryLevelSetTimer
{
    batteryLevelSecondsCount = 100;
    batteryLevelTimer = [NSTimer scheduledTimerWithTimeInterval:9.0 target:self selector:@selector(batteryLevelTimerRun) userInfo:nil repeats:YES];
}

In the .h file, I declared: .h文件中,我声明:

@interface MonitoringViewController : UIViewController

{
       IBOutlet UILabel *batteryLevelLabel; 
       IBOutlet UIProgressView *batteryLevel;

       NSTimer *batteryLevelTimer;
       int batteryLevelSecondsCount;
}

I assigned [self batteryLevelTimer] to a button press. 我将[self batteryLevelTimer]分配给按钮按下。 When I ran the code, I got an odd response in the batteryLevelLabel UILabel field. 运行代码时,在batteryLevelLabel UILabel字段中得到一个奇怪的响应。 It said Battery Level: -1% . 它说Battery Level: -1% Any idea why this would be? 知道为什么会这样吗? I also setup the UIProgressView to decrement along with label, and that is also dysfunctional (was set to 0), probably because it thinks that the value it was given was -1 so that it defaulted to 0 (UIProgressView value goes from 0.0 to 1.0). 我还将UIProgressView设置为随标签递减,这也是功能失调的(设置为0),可能是因为它认为给定的值是-1,所以默认为0(UIProgressView的值从0.0变为1.0) )。 Is there some mathematical/logic error I'm missing out on here? 我在这里错过了一些数学/逻辑错误吗?

Your code seems ok and works just fine in my test project (I connected button to batteryLevelSetTimer and label counted down to zero as expected). 您的代码似乎还可以,并且在我的测试项目中也可以正常工作(我将按钮连接到batteryLevelSetTimer并按预期将标签计数为零)。 Except for progress bar part -- you should change the line: 除了进度条部分-您应该更改以下行:

float batteryLevelProgressFloat = batteryLevelSecondsCount / 100;

to

float batteryLevelProgressFloat = batteryLevelSecondsCount / 100.0;

because both batteryLevelSecondsCount and 100 are integers, and integer division always yields integer result, which varies between 0 and 1 in your case. 因为batteryLevelSecondsCount100都是整数,并且整数除法总是产生整数结果,在您的情况下,结果在0到1之间变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM