현재 카운트 다운 타이머이 실행 중입니다. 내가 사용하는 코드는 다음과 같습니다.카운트 다운이 끝났을 때 알아보기
내가 도움이 필요한 것은 카운트 다운이 끝났을 때 (0 시간 남음) 알아내는 것입니다.
나는 그것을 알아낼 수 없다. 카운트 다운이 끝나면 푸시 알림을 보낼 수 있도록 가져와야합니다.
도움을 주셔서 감사합니다.
- (IBAction)startCountdown:(id)sender {
//Remove the time component from the datePicker. We care only about the date
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
self.datePicker.date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:self.datePicker.date]];
//Set up a timer that calls the updateTime method every second to update the label
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTime)
userInfo:nil
repeats:YES];
}
-(void)updateTime
{
//Get the time left until the specified date
NSInteger ti = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
NSInteger seconds = ti % 60;
NSInteger minutes = (ti/60) % 60;
NSInteger hours = (ti/3600) % 24;
NSInteger days = (ti/86400);
//Update the lable with the remaining time
self.countdownLabel.text = [NSString stringWithFormat:@"%02i days %02i hrs %02i min %02i sec", days, hours, minutes, seconds];
}
UPDATE 이것은 내가 지금까지 시도한 것입니다 :
- (void) bothDatesEqual {
if ([_countdownLabel.text isEqualToString:@"00 days 00 hrs 00 min 00 sec"]) {
NSLog(@"No time left");
}
else {
NSLog(@"Some time left");
}
}
업데이트 2 를이 내가 DANH
- (void)startCountdownDate {
//Remove the time component from the datePicker. We care only about the date
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
self.datePicker.date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:self.datePicker.date]];
[self updateTime];
NSTimer *timerDate;
timerDate = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTimeDate:)
userInfo:nil
repeats:YES];
}
-(void)updateTime
{
//Get the time left until the specified date
NSInteger ti = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
NSInteger seconds = ti % 60;
NSInteger minutes = (ti/60) % 60;
NSInteger hours = (ti/3600) % 24;
NSInteger days = (ti/86400);
//Update the label with the remaining time
_countdownLabel.text = [NSString stringWithFormat:@"%02i days %02i hrs %02i min %02i sec", days, hours, minutes, seconds];
}
- (void)updateTimeDate:(NSTimer *)timerDate {
NSDate *now = [NSDate date];
NSDate *targetDate = self.datePicker.date;
if (now == [now laterDate:targetDate]) {
// the current time is >= targetDate
[timerDate invalidate];
}
}
무엇을 시도 했습니까? 남은 시간을 계산할 수있는 것처럼 보입니다. 시간이 없는지 어떻게 확인 할 수 있습니까? – Jonah
조나, 응답 해 주셔서 감사합니다! 지금까지 시도한 것을 보여주기 위해 내 질문을 업데이트했습니다. 어떤 아이디어? – Realinstomp
그것은 시작이지만 그것이 효과가 있다고 생각하지 않는 이유는 무엇입니까? 어떤 d & d가 일어날 것을 기대하고 대신 무엇을 관찰 했습니까? 그게 무슨 뜻이라고 생각하니? "bothDatesEqual"이 호출 되었습니까? 그렇다면 당신이 기대하지 않은 것은 무엇입니까? – Jonah