0

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:

// 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:

@interface MonitoringViewController : UIViewController

{
       IBOutlet UILabel *batteryLevelLabel; 
       IBOutlet UIProgressView *batteryLevel;

       NSTimer *batteryLevelTimer;
       int batteryLevelSecondsCount;
}

I assigned [self batteryLevelTimer] to a button press. When I ran the code, I got an odd response in the batteryLevelLabel UILabel field. It said 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). Is there some mathematical/logic error I'm missing out on here?

5
  • Go through your code, line by line, by hand: what happens when batteryLevelSecondsCount is 0?
    – jscs
    Feb 16, 2014 at 4:55
  • @JoshCaswell When batteryLevelSecondsCount is 0, this executes: [batteryLevelTimer invalidate]; and batteryLevelTimer = nil;. I tried checking if it was calling that action by adding in a UIAlertView, but it didn't launch, which means batteryLevelSecondsCount is not setting to 0.
    – James
    Feb 16, 2014 at 5:14
  • What happens before that when batterLevelSecondsCount is 0?
    – jscs
    Feb 16, 2014 at 5:16
  • @JoshCaswell After using the same UIAlertView test, I found the value of batteryLevelSecondCount was set to -1. And that is the question, what does happen before? That's the question I'm trying to get an answer for.
    – James
    Feb 16, 2014 at 5:48
  • @user3125367 Thanks, now that's one less thing I have to worry about.
    – James
    Feb 16, 2014 at 8:49

1 Answer 1

0

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). 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.

4
  • Even after changing the 100 to 100.0, nothing changed with the UIProgressView. Beyond that, how would you explain the issue with the batteryLevelLabel?
    – James
    Feb 16, 2014 at 9:39
  • @James I think you should set some breakpoints and debug your program, because your code is working for me almostly as-is (i've ported it to AppKit to avoid mess with simulator). Set BPs on first lines of both methods and look what is going on. Feb 16, 2014 at 9:44
  • @James Or just NSLog(@"%d", batteryLevelLabel); to see what values does it become. Feb 16, 2014 at 9:45
  • The value that gets expressed in batteryLevelLabel is -1.
    – James
    Feb 16, 2014 at 10:16

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.