self.timerProgress=[NSTimer scheduledTimerWithTimeInterval:50.0 target:self selector:@selector(stopProgressView) userInfo:nil repeats:NO];
-(void)stopProgressView
{
if ([self.timerProgress isValid]) {
[self.timerProgress invalidate];
self.timerProgress=nil;
}
}
,並點擊一個按鈕,當我試圖作廢的NSTimer對象如何使NSTimer無效?
-(void)cancelTimer
{
if ([self.timerProgress isValid]) {
[self.timerProgress invalidate];
self.timerProgress=nil;
}
}
它沒有得到無效。它會在間隔50之後調用一次stopProgressView。 如何解決此問題?
「它在50間隔之後調用一次stopProgressView」 - 我無法理解這個問題。另外,什麼「問題」? – 2013-03-05 06:26:51
你的使用代碼在你的代碼中沒有問題。你的scheduledTimeWithTimeInterval:5秒不是50來測試你的代碼@ H2CO3是正確的 – 2013-03-05 06:27:44
我不明白你想在'stopProgressView'中做什麼,''selector '爲你的定時器:你正在創建一個非循環定時器,當它觸發時(它會觸發的唯一一次),你就無效了。何必?在'cancelTimer'中,很好,但在'stopProgressView'中是不必要的。在回答你的問題,爲什麼'cancelTimer'似乎沒有工作,你是否嘗試過使用該方法的斷點/日誌?你確定它被叫了嗎? (這不是'IBAction',所以我很懷疑。)底線,這是無效的正確方法,所以它不能被調用。 – Rob 2013-03-05 06:55:12