退出应用程序并重新启动计时器应根据分配的时间恢复时间

时间:2017-01-17 09:46:12

标签: ios objective-c xcode nstimer countdowntimer

我有倒数计时器。如果我退出应用程序并重新启动它,计时器应根据分配的时间恢复时间。可能吗?

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

- (void)timerFired
{
    if((currMinute>0 || currSeconds>=0) && currMinute>=0)
    {
        if(currSeconds==0)
        {
            currMinute-=1;
            currSeconds=59;
        }
        else if(currSeconds>0)
        {
            currSeconds-=1;
        }
        if(currMinute>-1)


        timeRemain=[NSString stringWithFormat:@"%d%@%02d",currMinute,@":",currSeconds];

    }
    else
    {
       [timer invalidate];
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个。

-(void)timerFired
{
    NSInteger currMin = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentMinute"];
    NSInteger currSec = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentSeconds"];

    if((currMin>0 || currSec>=0) && currMin>=0)
    {
        if(currSec==0)
        {
            currMin-=1;
            currSec=59;
        }
        else if(currSec>0)
        {
            currSec-=1;
        }
        if(currMin>-1)


            timeRemain=[NSString stringWithFormat:@"%d%@%02d",currMin,@":",currSec];
            [[NSUserDefaults standardUserDefaults] setInteger:currMin forKey:@"currentMinute"];
            [[NSUserDefaults standardUserDefaults] setInteger:currSec forKey:@"currentSeconds"];


    }
    else
    {
        [timer invalidate];
    }
}

答案 1 :(得分:0)

根据您的要求,您需要存储未来日期,如下所述。

在您启动计时器时添加当前时间并添加您的分钟和时间秒你需要什么时间。

例如:

if (![[NSUserDefaults standardUserDefaults] objectForKey:@"EndDate"]) {
   [[NSUserDefaults standardUserDefaults] setObject:dateFuture forKey:@"EndDate"];
}

现在将此未来日期存储到NSUserDefaults中,如此

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

启动计时器:

- (void)timerFired
{

    NSComparisonResult result = [[NSDate date] compare:[[NSUserDefaults standardUserDefaults] objectForKey:@"EndDate"]];
    if ( result == NSOrderedDescending) {
        [timer invalidate];
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"EndDate"];
    }else{
        NSTimeInterval secondsBetween = [[NSDate date] timeIntervalSinceDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"EndDate"]];

        NSLog(@"There are %f secondsBetween in between the two dates.", secondsBetween);
    }
}
  

注意:在启动计时器之前,检查UserDefaults中是否存在结束日期   和结束日期比当前日期更重要。

<p-dataTable #dt [value]="DataList" [rows]="10" [paginator]="true" [pageLinks]="4" [rowsPerPageOptions]="[10,20,50,100]" scrollable="true" scrollHeight="400px">
<p-column [style]="{'width':'134px','text-align':'right'}" field="UnitChange" header="Units Change" [sortable]="true"> </p-column>
<p-column [style]="{'width':'134px','text-align':'right'}" field="UnitPercentChange" header="% Change (Units)" [sortable]="true"></p-column>

现在,您可以在重新启动应用程序后获得剩余时间N次,您将只获得更新时间。