NSTimer在背景中 [英] NSTimer in background

查看:111
本文介绍了NSTimer在背景中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Iphone开发的新手。我有一个问题。我正在使用NSTimer,每秒更新一次UiLabel。现在我有两个问题:

I am very new in Iphone Development. I have an issue . I am using a NSTimer which update a UiLabel in every second. now i have two problem :


  1. 当我的应用程序进入后台时以及之后打开应用程序时。应用程序挂起。

  2. 如果我接下来或回到其他ui屏幕然后我来到计时器屏幕然后我的标签再次显示为0.

任何人都可以帮助我。

我正在使用的代码:

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];

-(void) updateCountdown
{
    secondsLeft--;

    //nits testing
    if(secondsLeft == 1)
    {
         [self.view addSubview:recipePage6View.view];
    }



    if (secondsLeft<0)
    {
        [timer invalidate];
        timer=nil;
        lblDisplayTimer.text =@"00:00:00";

    }
    else
    {
        hours = secondsLeft / 3600;
        minutes = (secondsLeft % 3600) / 60;
        seconds = (secondsLeft %3600) % 60;

        lblDisplayTimer.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
         //lblDisplayTimer.text = [NSString stringWithFormat:@"%02d:%02d",minutes,seconds];
     } 
}


推荐答案

你需要设置一个特殊的后台计时器任务,并且在主运行循环中没有NSTimer,它在后台暂停。有关如何执行此操作的文档是此处

You need to set up a special background timer task and not have an NSTimer on the main run loop which gets suspended in background. The documentation on how to do this is here

这篇关于NSTimer在背景中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆