3

ヨーロッパ旅行に出るまでの日数を表示するカウンターを作ろうとしています。たった70日(今日現在)なので、天文学的に大きな数字などを心配する必要はないと思いますが、本当に困惑しています-友達からもらったコードを添付しましたが、どちらも動作しません。私が考えることができるすべてを試したと言うとき、私を信じてください-そして、誰かが私の頭を噛む前に、私はこれらのフォーラムで行われているのを見ました、はい、私はAppleのドキュメントを非常に広範囲に調べましたが、私は100%ではありませんどこから始めればよいか-NSTimer、NSDate、およびそれらのすべてのサブクラスとメソッドを試しましたが、すぐに飛び出すものは何もありません。

私が実際にやるべきだと思うことに関しては、今日/今/現在の「日」に整数値を割り当てる必要があると思います。これは、を使用して動的に変化し[NSDate date]、その後、同じ日付に同じ値を割り当てます。離れる。メソッドが再度呼び出されたときにカウントダウンが更新され(必要に応じてNSTimerを使用してこれを行うことができます)、カウントダウンに表示される値は、これら2つの値の差です。

私は、私たちが去るまで毎秒更新されるような点滅するようなものは特に望んでいません-個人的にはそれは厄介だと思いますが、誰かが方法を知っているなら、将来の参考のためにそれをいただければ幸いです。

また、グーグルを広範囲に検索しました。単に間違った検索用語を使用している可能性がありますが、そこにも何も見つかりません。

どんな助けでも大歓迎です。

どうもありがとうございます。

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 に答える 2

4

2つの日付の間の秒単位の時刻(NSTimeInterval)がわかっている場合は、次のように、それをdays:hours:mins:secsの形式の文字列に簡単に変換できます。

- (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];
}
于 2010-07-05T09:11:10.647 に答える
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];
于 2011-02-25T12:08:49.613 に答える