我正在使用后台线程更新我的一个标签
我正在使用下面的代码。但在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;
}谢谢
发布于 2011-08-25 17:42:00
重新启动应用程序时,计时器将不起作用。您需要做的是通过appDelegate的applicationDidBecomeActive:方法重新初始化计时器,并确保通过appDelegate:方法关闭计时器
https://stackoverflow.com/questions/7187967
复制相似问题