从UI按钮点击获取时间戳并使用它们计算时差

时间:2014-01-09 03:12:21

标签: ios objective-c uibutton nsdateformatter

我找不到任何通过搜索专门解决这个问题的内容,所以这里就是......

我正在创建一个使用开始时间和停止时间作为元素的计算器。有一个开始按钮。单击时,它会抓取当前时间并使用hh:mm:ss:SS格式的抓取时间戳更新其旁边的标签。与停止时间按钮相同。

我需要做的是能够访问这些开始和停止时间,以便从停止时间中减去开始时间,从而获得在更大的计算中使用的时间。

对于那些比我更了解这一点的人来说,这似乎是一个简单的解决方法。我已经通过了日期&时间编程指南文档,它解决了其他类型的计算,但没有解决这个问题。

这是我的启动和停止按钮的实现代码:

     // startButton action - grabs start time and updates startTimeLabel
    - (IBAction)startButton:(UIButton *)sender
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"hh:mm:ss.SS"];
        _startTimeLabel.text = [formatter stringFromDate:[NSDate date]];
        UIButton *startButton = (UIButton *)sender;
        startButton.enabled = NO;
        stop.enabled = YES;
    }

    // stopButton action - grabs start time and updates stopTimeLabel, enables 
    calculate and resume buttons
    - (IBAction)stopButton:(UIButton *)sender
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"hh:mm:ss.SS"];
        _stopTimeLabel.text = [formatter stringFromDate:[NSDate date]];
        UIButton *stopButton = (UIButton *)sender;
        stopButton.enabled = NO;
        calculate.enabled = YES;
        // Hides keypad on calculateButton click
        [self.startLevel resignFirstResponder];
    }

以下是头文件中的开始和停止按钮的IBActions和Outlets:

    // ACTION for startButton. Grabs current time.
    // NEED TO DETERMINE HOW TO PUT THAT TIME IN THE CALCULATION
    - (IBAction)startButton:(UIButton *)sender;
    // OUTLET for startButton. Allows button state to be initialized.
    @property (strong, nonatomic) IBOutlet UIButton *start;


    // ACTION for stopButton. Grabs current time.
    // NEED TO DETERMINE HOW TO PUT THAT TIME IN THE CALCULATION
    - (IBAction)stopButton:(UIButton *)sender;
    // OUTLET for stopButton. Allows button state to be initialized.
    @property (strong, nonatomic) IBOutlet UIButton *stop;

感谢您的帮助,伙计们。

2 个答案:

答案 0 :(得分:0)

将开始日期和停止日期保存到ivars。

在停止操作(或计算操作,为什么需要额外操作?)中计算与[stopDate timeIntervalSinceDate:startDate];的差异


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (weak, nonatomic) IBOutlet UIButton *stopButton;
@property (weak, nonatomic) IBOutlet UILabel *timeElapsedLabel;
- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;
@end

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong)NSDate *startDate;
@property (nonatomic, strong)NSDate *stopDate;

@end

@implementation ViewController


- (IBAction)start:(id)sender
{
    [self.startButton setUserInteractionEnabled:NO];
    self.startDate = [NSDate date];
    [self.stopButton setUserInteractionEnabled:YES];
    self.timeElapsedLabel.text = @"";
}

- (IBAction)stop:(id)sender
{
    [self.stopButton setUserInteractionEnabled:NO];
    self.stopDate = [NSDate date];
    [self.startButton setUserInteractionEnabled:YES];

    self.timeElapsedLabel.text = [NSString stringWithFormat:@"%f", [self.stopDate timeIntervalSinceDate:self.startDate]];
}
@end

答案 1 :(得分:0)

正如另一张海报所说,将NSDates保存为实例变量。不要使用日期格式化程序转换为字符串。

就此而言,您可以保存NSTimeIntervals:

部首:

@interface MyVC
{
  NSTimeInterval startTime;
  NSTimeInterval endTime;
}
@end

.m文件:

- (IBAction)startButton:(UIButton *)sender
{
  startTime = [NSDate timeIntervalSinceReferenceDate];
}

- (IBAction)stopButton:(UIButton *)sender
{
  endTime = [NSDate timeIntervalSinceReferenceDate];
  NSTimeInterval elapsedSeconds = endTime - startTime;
  NSLog(@"It has been %.2f seconds since you clicked the start button");
}

我会给你更新你的开始和停止时间以及经过时间的标签。