2011-12-22 67 views
0

我有一個計時器用於我的項目,每次遞減1秒。但是如果計數器第二次開始工作,它會減少2秒,第三次減少3秒等等。我應該怎麼做才能讓所有時間減少1秒?對於每個頁面加載,計時器遞減量會有所不同

-(void)viewDidAppear:(BOOL)animated { 

    count=15; //timer set as 15 seconds 
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:)userInfo:nil repeats:YES]; //for decrementing timer 
} 

- (void)updateCounter:(NSTimer *)theTimer { 
    count -= 1; 
    NSString *s = [[NSString alloc] initWithFormat:@"%d", count]; 
    if(count==0) // alert for time expiry 
    { 
     alert = [[UIAlertView alloc] initWithTitle:@"Time Out!!" message:@"Your time is expired." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
     [alert show]; 
     self.timer.hidden=YES; 
     [self dismissModalViewControllerAnimated:YES]; 
    } 

    else { 
     self.timer.text = s; 
    } 

    [s release]; 
} 
+0

我的代碼是我的代碼是 - (無效)viewDidAppear:(BOOL)動畫 {計數= 15; //定時器設置爲15秒 [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:)userInfo:nil repeatats:YES]; //用於遞減定時器 } – priya 2011-12-22 07:10:44

+0

- (void)updateCounter:(NSTimer *)theTimer count - = 1; NSString * s = [[NSString alloc] initWithFormat:@「%d」,count]; if(count == 0)// alert time for expiry alert = [[UIAlertView alloc] initWithTitle:@「Time Out !!」消息:@「你的時間已過期。」 delegate:self cancelButtonTitle:@「Ok」otherButtonTitles:nil]; [alert show]; self.timer.hidden = YES; [self dismissModalViewControllerAnimated:YES]; } else { self.timer.text = s;} [s release]; } – priya 2011-12-22 07:10:51

+0

您可能會將您的代碼放入問題中,以使其可讀。 – 2011-12-22 07:12:23

回答

1

從你發佈的內容來看,你正在創建多個定時器,而不是停止它們中的任何一個。所以3次之後,你有3個定時器每秒發射。

至少,當計時器降爲零要使它無效:

[theTimer invalidate]

但你也可以考慮持有到您所創建的定時器(在@property),這樣你如果用戶在計數器實際爲零之前離開該視圖,可以使其失效並釋放它。

希望有所幫助。

+0

是的,這幫助了我。現在計時器工作正常。但是,當我使用後退按鈕時,如果我試圖阻止它並返回相同的問題重複,則計數器正在運行。我會爲此獲得解決方案嗎? – priya 2011-12-27 06:18:48

+0

好吧,每次你的視圖出現時你都會啓動一個新的計時器,所以當視圖消失時,你應該停止該計時器(使其無效)並釋放它(假設你保留了它)(viewWillDisappear :) – 2011-12-27 06:42:21

+0

請看這個http: //stackoverflow.com/questions/8641849/timer-decrement-gets-varied-for-each-page-load-when-using-back-button – priya 2011-12-27 06:48:43

0

編輯
(正如我貼這個我注意到你已經知道接受了上述的回答,似乎已刪除了你的「它不工作」從@Firoze Lafeer答案評論。但我會離開這個這裏以任何方式。)

運行下面的代碼作爲測試我不明白你的問題。即使我沒有失效,我只是在日誌中獲得了多個輸出。

而不是通過查看應用中的文本字段在做什麼來看看發生了什麼。嘗試並使用下面的日誌記錄,看看是否能讓您更好地瞭解發生的事情。

-(IBAction)runTimer:(id)sender { // attached to a button 
    [self invalidateTimer]; 
    count=15; //timer set as 15 seconds 
    //for decrementing timer 
    countimer = [NSTimer scheduledTimerWithTimeInterval:1.0f 
               target:self 
               selector:@selector(updateCounter:) 
               userInfo:nil 
               repeats:YES]; 
} 

-(void)updateCounter:(NSTimer *)theTimer { 
    count -= 1; 
    NSLog (@"count [email protected]%d", count);   
    if(count==0) // alert for time expiry 
    { 
     [self invalidateTimer]; 
    } 
} 

-(void)invalidateTimer { 
    if ([countimer isValid]) { 
     [countimer invalidate]; 
     NSLog(@"countimer invalidated "); 
     countimer = nil; 
    } 
} 
相關問題