2014-07-05 2 views
-1

코딩에 익숙하지 않습니다. 용서해주세요!NSDate 메서드 호출

배경 :

나는 일, 시간, 분, 초에 주어진 날짜에 카운트 다운 간단한 레이블 응용 프로그램을 가지고있다. 나는이 사용 NSDateNSTimer들했다 :

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    //Countdown timer 
    destinationDate = [NSDate dateWithTimeIntervalSince1970:1413961418]; 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; 
} 

-(void)updateLabel { 
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    int units = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; 
    NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0]; 

    [countdownLabel setText:[NSString stringWithFormat:@"%ld%c : %ld%c : %ld%c : %ld%c", (long)[components day], 'd', (long)[components hour], 'h', (long)[components minute], 'm', (long)[components second], 's']]; 
} 

문제 :

카운트 다운이 라벨에 도달

0 (즉, 우리는 주어진 날짜에 도달하면) 내가하는 방법은 숨어으로 화재로 얻을 수있는 방법 객체?

+0

사이드 노트 - 왜 당신은'h','m에 대한 '%의 c'를 사용하는 '등의 형식 문자열로? 콜론처럼 형식 문자열에 문자를 넣기 만하면됩니다. – rmaddy

답변

0

가장 좋은 방법은 현재 날짜가 destinationDate 이상인지 확인하는 것입니다.

if ([destinationDate timeIntervalSinceNow] <= 0) { 
    // The destination date is in the past - do something 
} 
+0

덕분에 완벽하게 작동했습니다. –

0

아마도 질문을받지는 못 하겠지만 라벨 텍스트가 "0 : 0 : 0 h.s"와 같은지 확인할 수 있습니까?

즉, 이미 updateLabel 메서드가 있습니다.

+0

예, 라벨은 주어진 날짜까지의 카운트와 같으므로 180 일 10 시간 30 분 2 초 및 매초 카운트 다운과 같은 내용이 표시됩니다. –

0

카운트 다운 타이머 조건 아래 다음 시도 제로에 도달하면 당신은 조건을 확인하려면 : -

 if ([destinationDate compare:[NSDate 
    date]]==NSOrderedSame) 

    //hiding your object 
    } 
+0

이것은 작동하지 않습니다. 날짜는 밀리 초 단위로 저장됩니다. 둘이 정확히 같을 확률은 본질적으로 0입니다. – rmaddy