iOS - 每秒都做一次动作

时间:2013-12-02 16:09:08

标签: ios objective-c nstimer

我希望每过一秒就做一次动作,例如我有一个标签,我希望它每秒都能做到这一点:

theLabel.text = @"Hey";

那么,我将如何做呢? 我想我需要使用NSTimer吗?

先谢谢

3 个答案:

答案 0 :(得分:4)

只需使用NSTimer:

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

并阻止它:

[timer invalidate];

在此处查找文档:

NSTimer Doc

答案 1 :(得分:2)

“B计划”解决方案:

[self performSelector:@selector(foo) withObject:nil afterDelay:{some time interval}];

-(void)foo
{
    //do something..
    [self performSelector:@selector(foo) withObject:nil afterDelay:{some time interval}];
}

答案 2 :(得分:1)

是的@kevin你是对的NSTimer是最好的

只需在加载时初始化你的计时器

NSTimer *timerCounter = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doMyWork) userInfo:nil repeats: YES]; //make repeats YES

并执行以下操作

-(void)doMyWork
{
   theLabel.text = @"Hey";
   // Or what ever you want to do
}