ios scheduleTimerWithTimeInterval 的时间量 [英] ios scheduledTimerWithTimeInterval for amount of time

查看:55
本文介绍了ios scheduleTimerWithTimeInterval 的时间量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 scheduleTimerWithTimeInterval 来在一定时间内执行周期性任务,比如说一小时.但是我如何在我的代码上实现.这是我的代码:

I want to use scheduledTimerWithTimeInterval to do a periodic task for certain amount of time lets say one hour. but How do I implement on my code. Here is my code:

timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
                                          target: self
                                        selector: @selector(doSomething:)
                                        userInfo: nil
                                         repeats: YES];

推荐答案

让我们假设一分钟.

这是一个简单的场景,

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

这篇关于ios scheduleTimerWithTimeInterval 的时间量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆