Arel
Arel

Reputation: 3938

Pausing app to display message for one second before dismissing a view

I have a simple timer that on completion shows the message "Rest Time Complete" I want that message to stay on the screen for one second, then dismiss a view. I've tried a few things that I'll detail below, but I can never get the message to display, pause, then dismiss the view. The timer pauses on the last number in the timer, then the message always appears as the view is being dismissed. Here is my code:

- (void)restTimerComplete
{
    self.restTimeLabel.text = @"Rest Period Complete!";
    [NSThread sleepForTimeInterval:1];
    [self hideRestTimerAnimated:YES];
}

I've also tried using the simple sleep(1), but that has the same end result.

Upvotes: 1

Views: 41

Answers (1)

rmaddy
rmaddy

Reputation: 318955

The simplest approach is to use dispatch_after:

- (void)restTimerComplete {
    self.restTimeLabel.text = @"Rest Period Complete!";
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hideRestTimerAnimated:YES];
    });

}

Upvotes: 1

Related Questions