2012-12-31 46 views
0

我的項目中有一個按鈕和一個標籤。單擊按鈕後,我需要啓動一個NSTimer,它顯示標籤上的值。例如1sec.I我不確定計時器是否返回value.is有沒有其他的方式來做到這一點?在標籤上顯示NSTimer值

按鈕的功能
+0

你可以參考這個[教程](http://iphoneapp-dev.blogspot.in/2010 /10/how-to-create-countdown-timer-in-iphone.html)它會幫助你 – thavasidurai

+0

感謝您的教程 – karthi

+0

你有沒有得到解決方案 – thavasidurai

回答

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]; 
} 
+0

你的方法錯了......請在有人降低你的評價之前糾正。 –

+0

哦..它應該是59 ..現在我糾正它/ –

+0

以及如何:'-showTimer {' –

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]; 
    } 
    } 
+0

我的價值每秒增加 – NANNAV

+0

請格式化您的代碼,爲什麼是60?所有的分鐘都沒有60秒。是的所有分鐘沒有60秒:) –

+0

定時器增加我一個接一個,它只獲取數字不是時間格式,所以用這個代碼轉換選項 – NANNAV