2011-08-25 7 views
0

배경 스레드를 사용하여 내 레이블 중 하나를 업데이트하려면배경 스레드 문제 아이폰

다음 코드를 사용 중입니다. 하지만 iOS 4.0에서는 응용 프로그램이 상태를 저장하고 백그라운드로 이동한다는 것을 알게되었습니다. 내 응용 프로그램도 그 작업을했지만 응용 프로그램을 숨길 때 작업 스레드가 멈추고 작업을 다시 시작하고 다시 열 때 왼쪽에서 다시 시작합니다. 아무도 내 스레드가 백그라운드에서 계속 실행하고 내 응용 프로그램이 숨겨져있는 동안 내 GUI를 변경하려면 코드에서 변경해야 할 필요가 있다고 말해 주시겠습니까? 나는 응용 프로그램의 재발 작동하지 않습니다 ..

-(void)startThread 
{ 


NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(setUpTimerThread) object:nil]; 


[thread start]; 

} 

-(void)setUpTimerThread 
{ 

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

NSTimer *timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(triggerTimer) userInfo:nil repeats:YES]; 

NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; 
[runLoop addTimer:timer forMode:NSRunLoopCommonModes]; 
[runLoop run]; 
[pool release]; 

} 

-(void)triggerTimer 
{ 

NSLog(@"***Timer Called after 3 seconds*** %d",count); 
self.label.text = [NSString stringWithFormat:@"count value = %d",count]; 
//self.titleLabel.text= [NSString stringWithFormat:@"count value = %d",count]; 
count = count +1; 
} 

감사

답변

1

타이머를이 코드를 사용하고 있습니다. 당신이해야 할 일은 appDelegate의 applicationDidBecomeActive : 메소드에서 타이머를 다시 초기화하고 applicationWillEnterBackground에서 타이머를 종료하는 것입니다 : method

+0

아니요. 제 잘못 이해하지 마세요. 백그라운드에서 스레드를 실행해야합니다 .... 내 스레드가 멈추길 원하지 않습니다 .... – Shah

+0

특정 범주에 속하지 않으면 스레드가 백그라운드에서 실행될 수 없습니다. 예를 들어 VOIP 애플리케이션은 백그라운드에서 실행될 수 있습니다. 여기에서 자세한 정보를 찾을 수 있습니다. http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html – Ved

+1

네,하지만 해결했습니다.이 스레드를 넣으면 그러면 Delegate Class는 iOS 4.0에서 스레드를 멈추지 않을 것입니다. 할당 된 모든 작업을 계속 수행합니다. :) 감사합니다 Ved를 통해 UR 도움말 :) – Shah