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?