IOS scheduledTimerWithTimeInterval的时间量

问题描述 投票:6回答:1

我想用scheduledTimerWithTimeInterval为一定量做周期性任务的时间可以说一小时。但如何在我的代码实现。这里是我的代码:

timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
                                          target: self
                                        selector: @selector(doSomething:)
                                        userInfo: nil
                                         repeats: YES];
ios nstimer
1个回答
6
投票

让我们假设一分钟。

下面是简单的场景,

int remainingCounts;
NSTimer *timer;

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:(self) selector:@selector(countDown) userInfo:nil repeats:YES];
remainingCounts = 60;   // int remainingCounts ;...

方法被调用每一个秒。

-(void)countDown {
    NSLog(@"%d", remainingCounts);
    // Do Your stuff......
    if (--remainingCounts == 0) {
        //[self dismissViewControllerAnimated:YES completion:nil]; 
        [timer invalidate];
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.