计算两次按下相同按钮之间经过的时间 [英] Calculate time elapsed between pressing the same button twice

查看:96
本文介绍了计算两次按下相同按钮之间经过的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试计算我在这个网站上找到的例子,但不知怎的,我每次都得到0值。我是IOS的新手,NSDate正在给我一个运行它的钱:)

I've tried all the time calculating examples I found on this site but somehow I'm getting 0 as value every time. I'm new to IOS and the NSDate is giving me a run for it's money :)

当我按下startStop按钮时,我想记录时间A,然后再次按下时记录时间B.再按一次(取消选择)必须计算这两个日期之间经过的时间。到目前为止我有这个:

I want to record time A when I press button "startStop", and then record time B when I press it again. Pressing it a second time (deselecting) has to calculate the time elapsed between these 2 dates. So far I have this:

-(IBAction)buttonClick {


    NSDate *startStopDate =  [NSDate alloc];

    NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"hh:mm:ss"];

    NSString *currentTime = [[NSString alloc] init];
    NSString *currentTime2 = [[NSString alloc]init];


    NSDate *start =[ [NSDate alloc]init];
    NSDate *stop = [[NSDate alloc] init];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    if (startStop.selected==NO) {

        NSLog(@"started");
        [startStop setSelected:YES];

        startStopDate = [NSDate date];


        currentTime = [formatter stringFromDate:startStopDate];

        NSLog(@"Current timestarted is %@",currentTime);


        startTime.text = currentTime;

        start = [formatter dateFromString:currentTime];


    }

    else {


        NSLog(@"Selected");
        [startStop setSelected:NO];

        startStopDate = [NSDate date];

        currentTime2 = [formatter stringFromDate:startStopDate];


        NSLog(@"Current time is %@",currentTime2);

        stopTime.text = currentTime2;
        stop = [formatter dateFromString:currentTime2];


        NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

        NSDateComponents *components = [gregorianCalendar components:unitFlags
                                                            fromDate:start
                                                              toDate:stop
                                                             options:0];



        NSInteger hours = [components hour];
        NSInteger minutes = [components minute];
        NSInteger seconds = [components second];


        NSLog(@"hello %d, %d, %d", hours, minutes, seconds);

    }
}

我的标签给出正确的小时:按下时的分钟输出,但我似乎无法将它们转移到日期计算。
我一直在尝试使用dateFromString和stringFromDate以及多个日期/字符串对象进行多种变换,但是到目前为止我只得到大的负数或者只有零。任何帮助表示赞赏;)

My labels give the correct hour:minute output when pressed, but I can't seem to get them to transfer to the date calculation. I've been trying numerous variations with dateFromString and stringFromDate and multiple date/string objects as you can see, but so far I only get big negative numbers or just zero as result. Any help appreciated ;)

Greets,Nick

Greets, Nick

推荐答案

那是因为start是一个局部变量。当第二次单击该按钮时,您的代码将跳过if块并直接转到else块,将启动变量分配但未初始化为有用的任何内容。如果要使用它来计算开始和停止时间之间的差异,则需要将此起始值存储在本地上下文之外。

That's because start is a local variable. When the button is clicked the second time, your code will skip the if block and go straight to the else block, leaving the start variable allocated but not initialized to anything useful. You need to store this start value outside of a local context if you want to use it to calculate the difference between the start and stop times.

这篇关于计算两次按下相同按钮之间经过的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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