2014-07-25 2 views
0

현재 카운트 다운 타이머이 실행 중입니다. 내가 사용하는 코드는 다음과 같습니다.카운트 다운이 끝났을 때 알아보기

내가 도움이 필요한 것은 카운트 다운이 끝났을 때 (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]; 
    } 
} 
+0

무엇을 시도 했습니까? 남은 시간을 계산할 수있는 것처럼 보입니다. 시간이 없는지 어떻게 확인 할 수 있습니까? – Jonah

+0

조나, 응답 해 주셔서 감사합니다! 지금까지 시도한 것을 보여주기 위해 내 질문을 업데이트했습니다. 어떤 아이디어? – Realinstomp

+0

그것은 시작이지만 그것이 효과가 있다고 생각하지 않는 이유는 무엇입니까? 어떤 d & d가 일어날 것을 기대하고 대신 무엇을 관찰 했습니까? 그게 무슨 뜻이라고 생각하니? "bothDatesEqual"이 호출 되었습니까? 그렇다면 당신이 기대하지 않은 것은 무엇입니까? – Jonah

답변

1

더 나은 방법에 따라 변경 것입니다 NSTimer를 사용하는 것은 시간의 근원이 아니라 펄스라고 생각하는 것입니다. 올바른 시간 원본은 [NSDate date]입니다. 그래서 진행 방식은 타이머를 설정하여 UI를 변경하는 경우 타이머 화재 및 테스트 :

// updateTime 
// give the selector a colon when you schedule the timer and when you implement it 
// that will give you access to the timer 

-(void)updateTime:(NSTimer *)timer { 

    NSDate *now = [NSDate date]; 
    NSDate *targetDate = self.datePicker.date; 

    if (now == [now laterDate:targetDate]) { 
     // the current time is >= targetDate 
     [timer invalidate]; 
    } 

    // the rest of your updateTime logic here, but see below... 

가 기억 선택기 당신이 예약 매개 변수 (콜론)을 얻었다 변경 ...

NSTimeInterval에서 시간 구성 요소를 추출하기위한보다 견고한 접근 방법 인 Here's a good SO answer.

EDIT - 시간에 대한 더 나은 답변은 정답 또는 최대 투표로 표시되지 않은 질문에 묻혀 있습니다. 본질적으로 ...

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *components = [gregorianCalendar components: (NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) 
                fromDate:targetDate 
                 toDate:now 
                options:0]; 
NSLog(@"%ld", [components year]); 
NSLog(@"%ld", [components month]); 
NSLog(@"%ld", [components day]); 
NSLog(@"%ld", [components hour]); 
NSLog(@"%ld", [components minute]); 
NSLog(@"%ld", [components second]); 
+0

응답 해 주셔서 감사합니다. 나는 이것으로 다소 추적하고 있다고 생각하지만 완전히 이해하려고 노력하고있다. 내가 현재 가지고있는 타이머를 타이머로 바꾸라고 하시겠습니까? – Realinstomp

+0

최소한의 단계로 타이머 코드에 터미널 조건을 추가하십시오. 그것은 정확히 targetDate에 도달했을 때, 나는 원래의 질문이라고 생각합니다. 그런 다음 그 시간에 타이머를 종료하고 싶으므로 콜론을 추가하여 매개 변수로 타이머를 포함 시키십시오. 마지막으로, 나중에 시간 구성 요소를 형식화하는 올바른 방법에 사용할 수있는 몇 가지 조언. – danh

+0

'타이머를 예약 할 때 셀렉터에 콜론을주고, 구현할 때 '내가 이미 작성한 코드에 대한 의미가 있습니까? 아니면 추가 한 내용에 대한 설명입니까? – Realinstomp

관련 문제