0

我的项目中有一个按钮和一个标签。单击按钮后,我需要启动一个在标签上NSTimer显示其值的按钮。例如 1 秒。我不确定计时器是否返回值。还有其他方法可以做到这一点吗?

4

2 回答 2

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];
    }
    }
于 2012-12-31T05:02:34.443 回答
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];
}
于 2012-12-31T04:51:18.803 回答