iphone - 我想要带重复计数的时间表计时器

标签 iphone objective-c ios

我想调用一个方法 24 次,但每次调用之间我想要 1 秒的间隔, 现在正在使用

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(loadNumber) userInfo:nil repeats:YES];

但是如何计算调用次数,我想在 24 次计数后使计时器无效我可以使它无效吗。

最佳答案

int 创建一个实例变量,不管你怎么调用它。在调用计时器之前将其设置为“0”。

someInt = 0;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(loadNumber:) userInfo:nil repeats:YES];

在时间调用的操作中,检查此数字的值以确保应执行操作并增加数字。

- (void)loadNumber:(NSTimer *)sender
{
    if (someInt <= 24) {
        //do something
        someInt ++;
    }else{
        [sender invalidate];
        someInt = 0;
    }
}

关于iphone - 我想要带重复计数的时间表计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13138111/

相关文章:

ios - 在 UILabel 中拟合文本 - Swift

iphone - iOS UIScrollView clipToBounds = 没有检测到 subview

java - 使用java处理应用内苹果收据

iphone facebook 登录 safari 打不开页面

iphone - 核心数据 : UIManagedDocument or AppDelegate to setup core data stack?

ios - 此应用目前无法使用 Facebook 登录

iphone - 使用核心数据存储对象

objective-c - 覆盖不使用 UITableView 滚动 - iOS

iphone - 取消 ASIHTTPRequest

ios - iOS 应用程序重新签名后未检测到应用程序