首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone中后台线程的问题

iPhone中后台线程的问题
EN

Stack Overflow用户
提问于 2011-08-25 17:25:50
回答 1查看 798关注 0票数 0

我正在使用后台线程更新我的一个标签

我正在使用下面的代码。但在iOS 4.0中,我了解到应用程序会保存其状态并转到后台。我的应用程序也做了这项工作,但是当我隐藏应用程序时,我正在使用的线程停止工作,当我重新打开它时,它又从我离开的地方恢复。谁能告诉我,我需要改变什么代码,使线程保持在后台运行,并改变我的GUI,而我的应用程序是隐藏的。我正在使用这个代码..

代码语言:javascript
复制
-(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;
}

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-25 17:42:00

重新启动应用程序时,计时器将不起作用。您需要做的是通过appDelegate的applicationDidBecomeActive:方法重新初始化计时器,并确保通过appDelegate:方法关闭计时器

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7187967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档