2009-08-20 4 views
1

는이 코드를 사용하고 있습니다 :NSTimer 문제

timer = [NSTimer scheduledTimerWithTimeInterval:2 
             target:self 
             selector:@selector(update) 
             userInfo:nil 
             repeats:YES]; 
... 

-(void)update { 
    NSDate *date = [NSDate date]; 
    NSString *Currentdate = [date descriptionWithCalendarFormat:@"%b %d, %Y %I:%M:%S %p" 
                 timeZone:nil 
                 locale:nil]; 
    lbl.text = Currentdate; 
} 

그것은 잘 작동하지만 런타임에 일초 2 초 타이머 간격을 변경하고 싶습니다. 어떻게해야합니까?

미리 감사드립니다.

+0

scheduledTimerWithTimeInterval : 1 대신 2? – diciu

+0

나는 타이머가 생성 된 후 타이머의 간격을 어떻게 바꿀지 알고 싶어한다고 생각한다. – jhauberg

답변

2

그냥 invalidate 이전 타이머와 새로 만듭니다. 나는 NSTimer이 시간 간격을 바꾸는 것을 지원하지 않는다고 생각한다. 그것은 인터페이스와 구현 모두에서 추가적으로 벼랑 일 것이다.

10

타이머 간격을 만든 후에 변경할 수 없습니다.

// You have to invalidate it...be careful, 
// this releases it and will crash if done to an already invalidated timer 

if (self.timer != nil) { 
    [self.timer invalidate]; 
    self.timer = nil; 


//... then declare a new one with the different interval. 

timer = [NSTimer scheduledTimerWithTimeInterval:newInterval 
            target:self 
            selector:@selector(update) 
            userInfo:nil 
            repeats:YES]; 
}