我有一个简单的秒表,它会给出从开始按钮被按下起经过几秒的时间。我需要的是从日期选择器设置开始时间。
如何获取“NSTimeInterval时间;”等于我的日期选择器?还是我需要做其他事情来设置用户的时间?
@implementation ViewController {
bool start;
// Gets time when the button is pressed.
NSTimeInterval time;
}
{
[super viewDidLoad];
self.display.text = @“0:00”;
开始=假;
}
-(无效)更新{
if (start == false) {
return;
}
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval elapsedTime = currentTime - time;
// minutes.
int minutes = (int)(elapsedTime / 60.0);
// seconds.
int seconds = (int)(elapsedTime = elapsedTime - (minutes * 60));
self.display.text = [NSString stringWithFormat:@"%u:%02u", minutes, seconds];
[self performSelector:@selector(update) withObject:self afterDelay:0.1];
}
最佳答案
您可以使用:
NSTimeInterval timePicker = [[self.datePicker date] timeIntervalSinceReferenceDate];
NSTimeInterval currentTime = [[NSDate date] timeIntervalSinceReferenceDate];
NSTimeInterval elapsedTime = currentTime - timePicker;
// minutes.
int minutes = (int)(elapsedTime / 60.0);
// seconds.
int seconds = (int)(elapsedTime = elapsedTime - (minutes * 60));