0

UIDatePicker (時刻のみ、日付なし) から UILocalNotification を設定するアラーム タイプのアプリを実装しようとしています。ユーザーがアラームを設定しようとしたときが午後 5 時であると仮定できるという事実を除いて、私はそれを機能させています。彼らが午後 4 時 45 分にアラームを設定しようとすると (次の日にこれを行うと仮定します)、UILocalNotification は設定されません。私のコード、特に「setHour」または「setMinute」、またはカレンダーでnsdatesを取得する方法が原因であると確信しています。誰かがそれを機能させるために何をする必要があるかを教えてくれれば幸いです。

UIDatePicker の名前は

   datePick

ありがとう!

UILocalNotification *futureAlert;
futureAlert = [[UILocalNotification alloc] init];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
//[dateComps setDay:item.day];
//[dateComps setMonth:item.month];
//[dateComps setYear:item.year];
[dateComps setHour:11];
[dateComps setMinute:46];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
futureAlert.fireDate = [datePick date];    
futureAlert.timeZone = [NSTimeZone defaultTimeZone];
futureAlert.alertBody= @"time to wake up";
[[UIApplication sharedApplication] scheduleLocalNotification:futureAlert];
[futureAlert release];
4

1 に答える 1

1

このコード行を使用してみてください:

double interval = [datePick.date timeIntervalSinceNow]; // time interval
if (interval < 0) // if time is earling at this time
  interval += 86400; // for set this time at next day
futureAlert.fireDate = [[NSDate date] dateByAddingTimeInterval: interval];
futureAlert.timeZone = [NSTimeZone systemTimeZone];

あなたの場合の間隔は85500秒に等しくなります。24 時間で 86400 秒、15 分で 900 秒、23 時間で 45 分で 85500 秒 (86400 - 900)。

したがって、間隔秒を計算して使用できます

于 2012-08-14T23:21:23.197 に答える