如何在小时和分钟上倒数计时器,并在它为0,目标c时停止

时间:2016-02-23 09:16:13

标签: ios objective-c xcode7

  viewdidload

 {   
    NSDateFormatter *df = [[[NSDateFormatter alloc] init] init];
    [df setDateFormat:@"HH:mm:ss"];

    ctime=([df stringFromDate:[NSDate date]]);
    NSDate *date1 = [df dateFromString:@"12:44"];`
    NSDate *date2 = [df dateFromString:@"20:50"];
    NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
    int hours = (int)interval / 3600;             // integer division to get the hours part
    int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
    NSString *timeDiff = [NSString stringWithFormat:@"%d:%02d", hours, minutes];
    currHour=hours;
    currMinute=minutes;
    currSeconds=00;
    [self start];
}

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

}
-(void)timerFired
{
    if((currHour>0 || currMinute>0 || currSeconds>=0) && currMinute>=0)
    {
        if(currSeconds==0)
        {

            currMinute-=1;
            currSeconds=59;
        }
        else if(currSeconds>0)
        {

            currSeconds-=1;
        }
        if(currMinute>-1)


enter code here
            [self.timeonelabel setText:[NSString stringWithFormat:@"%d%@%d%@",currHour,@":",currMinute,@":"]];
        //[self.hourlabel setText:[NSString stringWithFormat:@"%d%@",currHour,@":"]];
          //  [self.timeonelabel setText:[NSString stringWithFormat:@"%d%@%d%@",00,@":",currMinute,@":"]];
        [self.timetwolabel setText:[NSString stringWithFormat:@"%02d",currSeconds]];
    }
    else
    {
        [self.timer invalidate];
    }
}

在传递当前时间和结束时间时,计时器应该运行到倒计时到0。问题是计时器没有运行倒计时,请帮助。我甚至在tableview中显示计时器倒计时...请帮助

2 个答案:

答案 0 :(得分:1)

您的代码中有一些关于从字符串创建日期的错误。首先,NSDateFormatter中指定的日期格式与您使用的实际格式不匹配 - 一个有秒,另一个没有。这意味着dateFromString将返回nil。此外,您还应该指定一个日期,而不仅仅是时间,因为否则您无法确定今天的日期。

最后,通过提取和跟踪原始变量中NSDate的所有组件,您会使事情过于复杂。我宁愿做这样的事情:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"YYYY/MM/dd HH:mm"];
    self.endTime = [df dateFromString:@"2016/02/23 11:00"];

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

-(void)timerFired {
    if ([[NSDate date] compare:self.endTime] == NSOrderedAscending) {
        NSLog(@"still going");
        // enter code here
        // Use NSDateComponentsFormatter to show what you need to show
    }
    else {
        NSLog(@"done");
        [self.timer invalidate];
    }
}

答案 1 :(得分:1)

try this code  maybe this is your requirement 


- (void)viewDidLoad
{
    NSCalendar *g = [[ NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];


    NSDateComponents *d_comp1 = [[NSDateComponents alloc]init];
     // lower value for  time  compare to  d_comp time
    [d_comp1 setHour: 1]; 
    [d_comp1 setMinute: 1]; 
    [d_comp1 setSecond: 00];


    NSDateComponents *d_comp = [[NSDateComponents alloc]init];
    enter code here
    [d_comp setHour: 10];
    [d_comp setMinute: 1];
    [d_comp setSecond: 10];


    NSDate *date1 = [g dateFromComponents: d_comp1];

    NSDate *date2 = [g  dateFromComponents: d_comp];


            NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
        int hours = (int)interval / 3600;             // integer division to get the hours part
        int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
        int sec = (int)d_comp1.second - (int)d_comp.second;
    if (sec<0) {
        sec+=60;

    }
        currHour=hours;
        currMinute=minutes;
        currSeconds= sec;
        [self start];
}

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

}
 -(void)timerFired
{
        if(currHour>0 || currMinute>0 || currSeconds >0)
        {

            if (currSeconds < 0) {
                currSeconds = 60;
                currMinute -= 1;
            }
            if (currMinute < 0) {
                currHour -= 1;
                currMinute = 59;
                currSeconds =60;
            }
            [self.timeonelabel  setText:[NSString stringWithFormat:@"%d:%d:%d",currHour,currMinute,currSeconds]];
            currSeconds--;
        }
        else
        {
            [self.timeonelabel  setText:[NSString stringWithFormat:@"%d:%d:%d",currHour,currMinute,currSeconds]];

            [timer invalidate];
        }
}