0

現地時間の午後 6 時に UIAlertNotification を設定する必要があります。時刻は午前 11:00 に設定されており、現在のローカル タイム ゾーンは GMT+6:00 であるため、午後 18:00 になります。ただし、異なるタイムゾーンでは異なります。どの時間帯でも午後 6:00 になりたいです。誰でも親切に助けてもらえますか?ありがとう。

    NSDateComponents *comps=[[[NSDateComponents alloc]init]autorelease];
    [comps setYear:[year intValue]];
    [comps setMonth:[month intValue]];
    [comps setDay:[day intValue]];
    [comps setHour:[@"11" intValue]];//6pm
    [comps setMinute:0];
    [comps setMinute:0];
    [comps setTimeZone:[NSTimeZone localTimeZone]];
    NSCalendar *cal=[[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDate *date1=[[cal dateFromComponents:comps]retain];

// ローカル アラート コード

    NSInteger minutesAhead = 0;
    NSTimeInterval seconds = (60 * minutesAhead) * -1;
    NSDate *actualFireDate = [date1 dateByAddingTimeInterval:seconds];
    NSMutableDictionary *dictC = [NSMutableDictionary dictionaryWithDictionary:userInfo];
    [dictC setObject:date1 forKey:@"PCOriginalReminderDate"];

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = actualFireDate;
    notification.alertBody = message;
    notification.userInfo = dictC;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    [notification release];

ノート:

日付を印刷すると、次のように表示されます。

 date = "2012-04-04 18:00:00 +0000";

これは、午前 11 時が GMT であり、7 時間 (GMT +7 - 日光から) を追加することで、午後 18 時になることを意味すると思います。

4

1 に答える 1

2

あなたのtimeZone プロパティを設定したいUILocalNotification

fireDate で指定された日付は、このプロパティの値に従って解釈されます。nil (デフォルト) を指定すると、起動日は絶対 GMT 時間として解釈されます。これは、カウントダウン タイマーなどの場合に適しています。有効な NSTimeZone オブジェクトをこのプロパティに割り当てた場合、開始日は実時間として解釈され、タイム ゾーンが変更されたときに自動的に調整されます。この場合に適した例は、目覚まし時計です。

したがって、通知fireDateをスニペットで作成した時間に設定し、timeZoneそれをに設定すると、準備が整い[NSTimeZone localTimeZone]ます。

于 2012-03-22T00:05:44.450 に答える