Question

So I have a view controller with a timer. It has one button. When the view loads for the very first time, the button should say "start."

When it is tapped, "start"->"pause".

Another tap, "pause"->"resume".

Another tap, "resume"->"pause".

Since I want the timer to be accurate, I detached it from the main thread (I think I chose the right method, I would appreciate some clarification). But it seems like detaching it from the thread actually calls the method...which makes the button start with "pause" instead of start. How do I fix this?

By the way, default value (loads with) for testTask.showButtonValue is 1.

- (void)viewDidLoad {
    [super viewDidLoad];

    [NSThread detachNewThreadSelector:@selector(startTimer:) toTarget:self withObject:nil];

    if (testTask.showButtonValue == 1) {
        [startButton setTitle:@"Start" forState:UIControlStateNormal];
    } else if (testTask.showButtonValue == 2) {
        [startButton setTitle:@"Pause" forState:UIControlStateNormal];
    } else if (testTask.showButtonValue == 3){
        [startButton setTitle:@"Resume" forState:UIControlStateNormal];
    }
}
-(IBAction)startTimer:(id)sender{
    if (testTask.showButtonValue == 1) {
        [startButton setTitle:@"Pause" forState:UIControlStateNormal];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        testTask.showButtonValue = 2;
    } else if (testTask.showButtonValue == 2) {
        [startButton setTitle:@"Resume" forState:UIControlStateNormal];
        [timer invalidate];
        timer = nil;
        testTask.showButtonValue = 3;
    } else if (testTask.showButtonValue == 3){
        [startButton setTitle:@"Pause" forState:UIControlStateNormal];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        testTask.showButtonValue = 2;
    }
}
-(void)timerAction:(NSTimer *)t
{
    if(testTask.timeInterval == 0)
    {
        if (self.timer)
        {
            [self timerExpired];
            [self.timer invalidate];
            self.timer = nil;
        }
    }
    else
    {
        testTask.timeInterval--;
    }
    NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
    NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
                        seconds / 3600, (seconds / 60) % 60, seconds % 60];
    timerLabel.text = string;
    NSLog(@"%f", testTask.timeInterval);
}
Was it helpful?

Solution

I suggest going about this in a different way:

@interface SNDViewController ()
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (weak, nonatomic) IBOutlet UILabel *timerLabel;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSTimeInterval accumulativeTime;
@property (nonatomic, assign) NSTimeInterval currentReferenceTime;
@end

@implementation SNDViewController

EDIT: when the view loads, initialise self.accumulativeTime and update the timer label. The initialisation of accumulativeTime variable should really be done in the appropriate init* method, e.g. initWithNibName:bundle:. It is at this point that you would read the timer value from core data.

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.accumulativeTime = 300;
    [self updateTimerLabelWithTotalTime:self.accumulativeTime];
}

- (IBAction)changeTimerState:(UIButton *)sender
{
    if (self.timer == nil) {
        self.currentReferenceTime = [NSDate timeIntervalSinceReferenceDate];
        [self.timer invalidate];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
    } else {
        //Pause the timer and track accumulative time.
        NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
        [self.timer invalidate];
        self.timer = nil;
        NSTimeInterval timeSinceCurrentReference = now - self.currentReferenceTime;

EDIT: subtract timeSinceCurrentReference from accumalitiveTime, because this is a countdown timer. Also added a comment for saving to core data if necessary.

        self.accumulativeTime -= timeSinceCurrentReference;
        [self updateTimerLabelWithTotalTime:self.accumulativeTime];

        //Optionally save self.accumulativeTime to core data for future use.
    }

    NSString *buttonTitle = (self.timer != nil) ? @"Pause" : @"Resume";
    [self.startButton setTitle:buttonTitle forState:UIControlStateNormal];
}

You don't need to store any unnecessary state, such as the showButtonValue variable. Instead you can base your decision of whether to pause or resume on whether or not self.timer == nil. If there is no timer running, then grab the current reference time and start a new timer. The timer is scheduled to fire every 0.01 seconds, which will hopefully make it accurate to 0.1 seconds. You never need to change the button's title to "Start". It is either "Pause" or "Resume". When the timer is paused by the user, we dispose of self.timer and update the timer label with the most accurate time.

- (void)updateTimer:(id)sender
{
    NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval timeSinceCurrentReference = now - self.currentReferenceTime;

EDIT: subtract timeSinceCurrentReference from self.accumulativeTime to get the totalTime (i.e. totalTime decreases as time goes by).

    NSTimeInterval totalTime = self.accumulativeTime - timeSinceCurrentReference;

    if (totalTime <= 0) {
        totalTime = 0;
        [self.timer invalidate];
        self.timer = nil;

        //What to do when we reach zero? For example, we could reset timer to 5 minutes:
        self.accumulativeTime = 300;
        totalTime = self.accumulativeTime;
        [self.startButton setTitle:@"Start" forState:UIControlStateNormal];
    }

    [self updateTimerLabelWithTotalTime:totalTime];
}

Each time the timer is fired, we get the total time by finding the difference between now and self.currentReferenceTime and add it to self.accumulativeTime.

- (void)updateTimerLabelWithTotalTime:(NSTimeInterval)totalTime
{
    NSInteger hours = totalTime / 3600;
    NSInteger minutes = totalTime / 60;
    NSInteger seconds = totalTime;
    NSInteger fractions = totalTime * 10;

    self.timerLabel.text = [NSString stringWithFormat:@"%02u:%02u:%02u.%01u", hours, minutes % 60, seconds % 60, fractions % 10];
}

@end

The method - (IBAction)changeTimerState:(UIButton *)sender is called by the UIButton on the "Touch Up Inside" event (UIControlEventTouchUpInside).

You don't need to do anything in viewDidLoad.

Also, and importantly, this is all done on the main thread. If anything gets in the way of the main thread updating the timer label, then the text visible to the user may be inaccurate, but when it is updated, you can be sure that it will be accurate again. It depends on what else your app is doing. But since all UI updates must be done on the main thread there is really no avoiding this.

Hope this helps. Let me know if anything is unclear.

(Xcode project available here: https://github.com/sdods3782/TVTTableViewTest)

OTHER TIPS

Calling detachNewThreadSelector will create a new thread and perform the selector mentioned, immediately afer the call.

For fixing your issue change your methods like:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [NSThread detachNewThreadSelector:@selector(startTimer:) toTarget:self withObject:nil];
}
-(IBAction)startTimer:(id)sender
{
    if (testTask.showButtonValue == 1) {
        [startButton setTitle:@"Start" forState:UIControlStateNormal];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        testTask.showButtonValue = 3;
    } else if (testTask.showButtonValue == 2) {
        [startButton setTitle:@"Resume" forState:UIControlStateNormal];
        [timer invalidate];
        timer = nil;
        testTask.showButtonValue = 3;
    } else if (testTask.showButtonValue == 3){
        [startButton setTitle:@"Pause" forState:UIControlStateNormal];
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        testTask.showButtonValue = 2;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top