12시 01 분 00 초, 12시 2 분 0 초 ...iOS 분마다 메서드 호출 방법
iOS7에서 분 시간이 변경되면 메서드를 호출하고 싶습니다. . 즉, 지금 01:55:47이면 01:56:00 -> 01:57:00 -> 01:58:00 ...
모두를 호출하는 방법입니다. 메서드에 대한 호출은 TimeInterval에 관한 것입니다.하지만 원하는 것은 아닙니다. 나를 도울 수
0 초 희망 멋진 사람들에
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!");
}
그러나이 방법은 호출되지 않습니다 :
나는이 코드를 시도했습니다!
----- 내 대답 -------
나는 또한 내 코드가 제대로 작동하지 않는 이유는, 내가의 정확한 시간입니다 roundedInterval에 추가로 60 초 동안 추가 필요 알아낼다음 분. 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];
이제 작동합니다!
1 초 간격으로 타이머를 설정합니다. 이제 초가 00이면 델리게이트 함수 내부를 확인할 수 있습니다. 그렇다면 함수를 호출하십시오. 또는 적절한 때에 델리게이트에 전화하십시오. 나는 00시에 델리게이트에 전화해야한다는 것을 의미합니다. –