我的意思是12:01:00,12:02:00 ...iOS如何在分钟,每分钟拨打一个方法
在iOS7中,我想在分钟时间改变时调用方法。换句话说,如果现在是01:55:47,那么我想在01:56:00 - > 01:57:00 - > 01:58:00打电话给方法...
我找到的所有关于调用方法只是关于TimeInterval,但不是我想要的。
我试过这段代码:
NSTimeInterval roundedInterval = round([[NSDate date] timeIntervalSinceReferenceDate]/60.0) * 60.0;
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:roundedInterval];
NSTimer *timer = [[NSTimer alloc] initWithFireDate:date
interval:60.0
target:self
selector:@selector(handleEveryMinutes:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
- (void)handleEveryMinutes:(NSTimer *)timer {
NSLog(@"one minute!");
}
但这种方法不叫在第0秒
希望冷静的人能帮帮我!
-----我的回答-------
我也明白,为什么我的代码无法正常工作,我需要额外60秒添加到roundedInterval,这是确切的时间下一分钟。如果不添加60秒,fireDate就会被传递,所以当我运行我的应用程序时,它必须立即启动。
NSTimeInterval roundedInterval = round([[NSDate date] timeIntervalSinceReferenceDate]/60.0) * 60.0 + 60; // the extra 60 is used to set the correct time Interval between next minute and referenced date.
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:roundedInterval];
现在,它的工作!
您的计时器设置为一秒的间隔。现在,如果秒数为00,则可以在委托函数内部检查。如果是这样,请调用您的函数。或在正确的时间打电话给你的delgate。我的意思是你必须打电话给你的delgate在00. –