我正在使用后台线程来更新我的标签之一
我正在使用以下代码。但在 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;
}
Thanks
重新启动应用程序时计时器将不起作用。您需要做的是从 appDelegate 的 applicationDidBecomeActive: 方法重新初始化计时器,并确保从 applicationWillEnterBackground: 方法关闭计时器
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)