2010-07-05 276 views
3

我試圖做一個計數器,它顯示了我們離開歐洲之前的天數。這只是大約70天(截至今天),所以我不認爲我應該擔心天文數字或任何事情,但我真的很難過 - 我附上了一些朋友給我的代碼,這些代碼也不行。當我說我已經嘗試過所有我能想到的事情時,請相信我 - 在任何人在我這些論壇上看到完成的事情之前,任何人都會咬我的頭,是的,我在Apple文檔中看到了非常廣泛的內容,但我不是100%確定從哪裏開始 - 我已經嘗試過NSTimer,NSDate及其所有的子類和方法,但沒有什麼能夠立即跳出來。倒計時 - iPhone倒數計時器

在我認爲我應該做的事情方面,我想我需要以某種方式爲今天/現在/當天的「日期」分配一個整數值,這將使用[NSDate date]動態更改,然後相同我們離開的日期。當方法被再次調用時,倒計時只是更新(如果需要,我可以使用NSTimer來執行此操作),並且倒計時中顯示的值是這兩個值之間的差異。

我不是特別想要一種閃爍的東西,每秒更新一次,直到我們離開 - 我個人認爲這很俗氣,但如果有人知道如何,那麼我會很感激它爲未來的參考。

我也做了大量的谷歌搜索,我可能只是使用錯誤的搜索條件,但我無法找到任何東西。

任何幫助,非常感謝。

非常感謝。

Michaeljvdw

- (void)countDownMethod { 
NSDateComponents *comps = [[NSDateComponents alloc] init]; 
[comps setDay:startDay]; 
[comps setMonth:startMonth]; 
[comps setYear:startYear]; 
[comps setHour:startHour]; 

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDate *date = [gregorian dateFromComponents:comps]; 
NSLog(@"%@",date); 
[gregorian release]; 
[comps release]; 
NSTimeInterval diff = [date timeIntervalSinceNow]; 

int diffInt = diff; 

NSString *days = [NSString stringWithFormat:@"%d",diffInt/86400]; 
day0.text = @"0"; 
day1.text = @"0"; 
day2.text = @"0"; 
NSLog(@"Days Length: %d",days.length); 

if(days.length >= 1){ 
    day2.text = [days substringFromIndex:days.length - 1]; 

    if(days.length >= 2){ 
     day1.text = [days substringWithRange:NSMakeRange(days.length - 2, 1)]; 

     if(days.length >= 3){ 
      day0.text = [days substringWithRange:NSMakeRange(days.length - 3, 1)]; 
     } 
    } 
} 

NSString *hours = [NSString stringWithFormat:@"%d",(diffInt%86400)/3600]; 
hour0.text = @"0"; 
hour1.text = @"0"; 
NSLog(@"Hours Length: %d",hours.length); 

if(hours.length >= 1){ 
    hour1.text = [hours substringFromIndex:hours.length - 1]; 

    if(hours.length >= 2){ 
     hour0.text = [hours substringWithRange:NSMakeRange(hours.length - 2, 1)]; 
    } 
} 
NSString *minutes = [NSString stringWithFormat:@"%d",((diffInt%86400)%3600)/60]; 
minute0.text = @"0"; 
minute1.text = @"0"; 
NSLog(@"Minutes Length: %d",minutes.length); 

if(minutes.length >= 1){ 
    minute1.text = [minutes substringFromIndex:minutes.length - 1]; 

    if(minutes.length >= 2){ 
     minute0.text = [minutes substringWithRange:NSMakeRange(minutes.length - 2, 1)]; 
    } 
} 

}

回答

4

如果你知道在2個日期(您NSTimeInterval),那麼你可以很容易地轉換到這一點在格式天字符串之間的秒數:小時:分鐘:秒如下。

- (NSString*)secsToDaysHoursMinutesSecondsString:(NSTimeInterval)theSeconds { 
div_t r1 = div(theSeconds, 60*60*24); 
NSInteger theDays = r1.quot; 
NSInteger secsLeftFromDays = r1.rem; 

div_t r2 = div(secsLeftFromDays, 60*60); 
NSInteger theHours = r2.quot; 
NSInteger secsLeftFromHours = r2.rem; 

div_t r3 = div(secsLeftFromHours, 60); 
NSInteger theMins = r3.quot; 
NSInteger theSecs = r3.rem; 

NSString* days; 
if (theDays < 10) { // make it 2 digits 
    days = [NSString stringWithFormat:@"0%i", theDays]; 
} else { 
    days = [NSString stringWithFormat:@"%i", theDays]; 
} 

NSString* hours; 
if (theHours < 10) { // make it 2 digits 
    hours = [NSString stringWithFormat:@"0%i", theHours]; 
} else { 
    hours = [NSString stringWithFormat:@"%i", theHours]; 
} 

NSString* mins; 
if (theMins < 10) { // make it 2 digits 
    mins = [NSString stringWithFormat:@"0%i", theMins]; 
} else { 
    mins = [NSString stringWithFormat:@"%i", theMins]; 
} 

NSString* secs; 
if (theSecs < 10) { // make it 2 digits 
    secs = [NSString stringWithFormat:@"0%i", theSecs]; 
} else { 
    secs = [NSString stringWithFormat:@"%i", theSecs]; 
} 

return [NSString stringWithFormat:@"%@:%@:%@:%@", days, hours, mins,secs]; 
} 
+0

非常感謝你,這完美的作品。非常感激! – Michaeljvdw 2010-07-06 04:41:27

2
//Another simple way to get the numbers of days difference to a future day from today. 

NSTimeInterval todaysDiff = [todayDate timeIntervalSinceNow]; 
NSTimeInterval futureDiff = [futureDate timeIntervalSinceNow]; 
NSTimeInterval dateDiff = futureDiff - todaysDiff; 

div_t r1 = div(dateDiff, 60*60*24); 
NSInteger theDays = r1.quot; 

label.text = [NSString stringWithFormat:@"%i", theDays];