我正在開發一個iOS應用程序,它可以從應用程序提供/發出呼叫。我們可以一個接一個地打兩個電話。我們第一次打第一個電話。一旦通話建立起來,NSTimer應該被解僱,它會顯示通話的持續時間。使用2個NSTimers時發生的問題
對於這個我建立這個第一個電話後做以下定時器
self.switchTimer1 = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(setTimerLabel1:)
userInfo:nil
repeats:YES];
[self.switchTimer1 fire];
,用戶可以讓第二個電話。
第二次通話建立後,它會觸發第二個計時器。
if (hasSecondCall)
{
self.switchTimer2 = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(setTimerLabel2:)
userInfo:nil
repeats:YES];
timeSec=0;
timeMin=0;
[self.switchTimer2 fire];
}
它在第一次打電話時工作正常。
想,如果我結束第二個呼叫,再次我建立呼叫,計時器會自動調用並呼籲立即成立之前進行的呼叫,那個時候第二個定時器,定時器的值遞增速度非常快如雙值顯示。就像,2,4,6等
燒成方法後secondtimer以下
- (void)setTimerLabel2:(NSTimer *)timer {
timeSec=timeSec+1;
NSLog(@"timeSec+1 %d",timeSec+1);
if (timeSec == 60)
{
timeSec = 0;
timeMin=timeMin+1;
}
NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
[_switchCallLCD setStatus:timeNow labelNumber:2];
}
雖然斷開電話我們調用下面就這個問題
- (void)endingCallWithId:(UInt32)call_id {
if (hasSecondCall&& call_id==_current_call) {
if (self.switchTimer2) {
//NSLog(@"self.predictNumber %@",self.predictNumber);
self.predictNumber=self.predictNumber2;
[_lcd setText:self.predictNumber];
[self.switchTimer2 invalidate];
self.switchTimer2 = nil;
[self.switchTimer2 release];
[_switchCallLCD setStatus:NSLocalizedString(@"call ended",nil) labelNumber:2];
timeSec = 0;
timeMin = 0;
}
找遍了這麼多論壇,但我找不出解決方案。我聽說,如果我們使用多個定時器,就會出現這樣的問題。
@Anna感謝編輯我的問題 – 2015-03-13 10:56:32
什麼是'_current_call','timeSec'和'timeMin',實例變量?通常我們使用ivars的屬性。 「endingCallWithId」在哪裏被調用?看起來你沒有使用ARC('[self.switchTimer2 release]'),這不是一個好主意。 – zaph 2015-03-13 11:10:49
我的應用程序有一些文件弧和一些非弧,但這個文件是非弧//在標題我宣佈這些,雖然我釋放他們在dealloc方法,沒有任何變化的定時器狀態 int timeSec = 0; int timeMin = 0; – 2015-03-13 11:13:23