我希望每过一秒就做一次动作,例如我有一个标签,我希望它每秒都能做到这一点:
theLabel.text = @"Hey";
那么,我将如何做呢? 我想我需要使用NSTimer吗?
先谢谢
答案 0 :(得分:4)
只需使用NSTimer:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doSomething:) userInfo:nil repeats: YES];
并阻止它:
[timer invalidate];
在此处查找文档:
答案 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
}