简体   繁体   中英

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.

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];
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM