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?

Community
  • 1
  • 1
James
  • 717
  • 2
  • 6
  • 9
  • Go through your code, line by line, by hand: what happens when `batteryLevelSecondsCount` is 0? – jscs Feb 16 '14 at 04: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 '14 at 05:14
  • What happens _before_ that when `batterLevelSecondsCount` is 0? – jscs Feb 16 '14 at 05: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 '14 at 05:48
  • @user3125367 Thanks, now that's one less thing I have to worry about. – James Feb 16 '14 at 08:49

1 Answers1

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.

user3125367
  • 2,920
  • 1
  • 17
  • 17
  • 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 '14 at 09: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. – user3125367 Feb 16 '14 at 09:44
  • @James Or just `NSLog(@"%d", batteryLevelLabel);` to see what values does it become. – user3125367 Feb 16 '14 at 09:45
  • The value that gets expressed in `batteryLevelLabel` is `-1`. – James Feb 16 '14 at 10:16