我的项目中有一个按钮和一个标签。点击按钮后,我需要启动NSTimer
,在标签上显示它的值。例如1sec。我不确定计时器是否返回值。我在那里还有其他方法吗?。
答案 0 :(得分:0)
在按钮功能中单击
{
NSTimer *t;
t=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showTimer) userInfo:nil repeats:YES];
}
showTimer函数中的
-(void)showTimer{
static int i=0;
static int min=0;
if(i>=59){
i=0;
min++;
} else {
i++;
}
yourLabel.text=[NSString stringWithFormat:@" %d:%d ",min,i];
}
答案 1 :(得分:0)
NSTimer *aTimer = [NSTimer timerWithTimeInterval:(1) target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:aTimer forMode: NSDefaultRunLoopMode];
int i=0;
- (void)timerFired:(NSTimer*)theTimer
{
if(condition)
{
// timer repeat
// your actions
i++;
int conseconds = i % 60;
int conminutes = (i / 60) % 60;
timelable.text=[NSString stringWithFormat:@"%02d:%02d", conminutes,conseconds];
[theTimer isValid];
}
else
{
// your actions
//timer stopped
[theTimer invalidate];
}
}